What you can do after this lesson
You will create separate lines of work, move between them, and tidy them up. Branches are what make Git comfortable to experiment in, so it is worth understanding what they really are. We use a fictional library website for the examples.
A branch is a label, nothing more
People imagine a branch as a copy of the project. It is not. A branch is a small named pointer to one commit. Git makes a branch by writing that commit's hash into a tiny file. That is why creating a branch is instant even in a huge project.
HEAD is a second pointer that says which branch you are currently on. When you make a commit, the new commit is attached to the commit you were on, and the branch that HEAD is on moves forward to include it. Other branches stay where they were. That single rule explains everything in this section.
Start with a repository holding two commits:
git branch* mainThe star marks the current branch. Create another, which is only a new label on the same commit:
git branch opening-hours-update
git branch -v* main 594fee7 Add opening hours
opening-hours-update 594fee7 Add opening hoursBoth labels point at 594fee7. Example hashes; yours differ.
Switching
Move to the new branch with git switch:
git switch opening-hours-update
git log --oneline --decorateSwitched to branch 'opening-hours-update'
594fee7 (HEAD -> opening-hours-update, main) Add opening hours
e6583be Add home pageHEAD -> opening-hours-update shows that HEAD points at the branch. Now commit something:
echo "Opening hours: 9 to 6." > hours.txt
git commit -am "Extend hours to 6"
git switch main
cat hours.txtOpening hours: 9 to 5.Back on main, the file shows the old text. Switching rewrites your working files to match the branch you moved to, which is why Git insists on a tidy state: if you have uncommitted changes that would be overwritten, it refuses and tells you. Commit or stash first.
(git switch arrived in Git 2.23. The older git checkout <branch> still works and does the same here.)
Create and switch in one move
The most common form is -c (create):
git switch -c new-books
echo "New: Persuasion" > books.txt
git add .
git commit -m "Add new books page"
git log --oneline --graph --decorate --all* bb0b6f1 (HEAD -> new-books) Add new books page
| * 788946b (opening-hours-update) Extend hours to 6
|/
* 594fee7 (main) Add opening hours
* e6583be Add home pageThe graph shows history forking at 594fee7: two branches each have one commit the other lacks. --all includes every branch, not just the current one.
Handy shortcuts: git switch - jumps back to the previous branch, and git branch --show-current prints the current branch name.
Naming branches
Branch names are free text without spaces. Teams usually choose a convention, such as feature/search, fix/login-bug or docs/update-readme. A slash is allowed and groups them. Short, descriptive and lowercase works well. The main branch (main in new repositories) holds the version your team treats as the stable line.
Renaming and deleting
git branch -m opening-hours-update hours-update
git branch -d new-bookserror: the branch 'new-books' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D new-books'-m renames. Deleting with -d is deliberately cautious: it refuses to delete a branch whose commits are not reachable from your current branch, since the branch is your only label for that work. If you decide the work is not needed, git branch -D new-books deletes it regardless:
Deleted branch new-books (was bb0b6f1).Note the hash. If you delete the wrong branch, git branch new-books bb0b6f1 restores it, and the previous lesson's reflog technique also finds it. You cannot delete the branch you are currently on; switch away first.
Detached HEAD, briefly
If you check out a commit or tag directly, for example git switch --detach v1.0.0, HEAD points at that commit rather than a branch. You can look around, but any commit you make there belongs to no branch and is easy to lose. If you want to keep such work, create a branch there with git switch -c name.
Habits that keep branches useful
Create a branch for each task, however small, because it costs nothing and keeps main stable. Give it a name that says what it is for, so the list from git branch reads like a to-do list of work in flight. Check where you are with git status before you start typing commits. And when a branch has done its job and is merged, delete it. A short list of live branches is easy to reason about, while dozens of forgotten ones hide the work that actually matters.
Common mistakes
- Committing on the wrong branch. Run
git branchorgit statusbefore you start; the output names the current branch. - Switching with uncommitted changes that conflict. Commit them, or stash them in lesson 22.
- Treating a branch as a backup copy. Branches share history; they are not independent copies of the files.
- Letting branches pile up forever. Delete merged branches to keep the list readable.
- Using
-Dwhen-dcomplained. The warning was telling you the branch holds unmerged work.
Exercise
- In a practice repository, create a branch called
about-pageand switch to it. - Add a file
about.txt, commit it, and switch back tomain. Is the file there? - Draw the history with
git log --oneline --graph --decorate --all. - Delete the branch. What does Git say, and why?
Show one possible answer
git switch -c about-page
echo "About us" > about.txt
git add about.txt && git commit -m "Add about page"
git switch main
ls
git log --oneline --graph --decorate --all
git branch -d about-pageOn main, about.txt is not listed, because the commit lives only on about-page. The -d delete refuses with "not fully merged" until you merge the branch, which the next lesson covers.
Key points
- A branch is a lightweight label pointing at a commit; creating one copies no files.
- HEAD says which branch you are on; a new commit moves that branch's label forward.
- git switch -c <name> creates and switches in one step; git switch - returns to the previous branch.
- git branch -d deletes a merged branch and refuses to delete unmerged work; -D forces it.
- git log --oneline --graph --decorate --all draws every branch so you can see how they diverge.