Branches are just pointers
A branch is a sticky note on a commit. That is all it is.
1 Learn the idea
People imagine a branch as a copy of the project, or as a separate folder. It is neither. A branch is a name that points at one commit — and nothing more.
HEAD: "you are here"
HEAD is a special pointer that says which branch you are currently on. When you commit, git creates the new commit with the current commit as its parent, then moves the branch that HEAD points to. Only that one branch moves.
- Creating a branch is instant — git just writes a 41-byte file containing a hash. No files are copied.
- Switching branches moves HEAD and swaps the files in your folder to match that commit's snapshot.
- Deleting a branch removes the sticky note, not the commits (though commits nobody points to are eventually cleaned up).
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git branch
Two commits, one branch.
mainpoints at the latest commit, andHEADpoints atmain(the*in the list).$ git branch soup
git branch soupsticks a new note onto the same commit. No files copied, no new commits — the graph barely changes.$ git switch soup
git switchmoves HEAD tosoup. Both branches still point at the same commit, so your files do not change.$ echo "Tomato soup: tomatoes, onion, basil" > soup.md $ git add soup.md $ git commit -m "Add tomato soup"
We commit on
soup. Watch: only the soup note moved forward.mainstayed behind, because HEAD was not pointing at it.$ git switch main
Back on
main. HEAD jumps back, andsoup.mddisappears from the working directory — main's snapshot doesn't have it. It is safe inside the soup commit.$ echo "Waffles: flour, eggs, butter" > waffles.md $ git add waffles.md $ git commit -m "Add waffles"
A commit on
main. History now forks: two branches grew in different directions from a common ancestor.$ git switch -c dessert
git switch -c= create a branch and switch to it in one go. The most common way to start new work.$ git switch main $ git branch -d dessert
desserthad no unique commits, so deleting it is harmless. Deleting a branch removes the note, never the shared commits.$ git branch -d soup
Git refuses to delete
soup: its commit is not part ofmain, so the note is the only thing keeping it findable.-Dwould force it.
- A branch is a copy of my files
- Branches are expensive, make few
- Deleting a branch deletes its work immediately
- A branch is a pointer to one commit
- Branches cost ~nothing — make one per task
- It deletes the pointer; git warns if commits would be orphaned
git branchList branches (* marks where HEAD is)git switch -c <name>Create a branch here and move onto itgit switch <name>Move HEAD to another branchgit branch -d <name>Delete a (merged) branch pointer3 Check your understanding
What is a branch in git, technically?
You are on feature (see graph). You make a new commit. Which labels move?
Why does a file sometimes "disappear" when you git switch to another branch?
git branch -d experiment fails with "not fully merged". What does that tell you?
4 Practice for real
Work on a side branch
Create a branch called feature, make one commit on it, and then return to main. main must not contain the new commit.