Git Branches and HEAD: Understand Movable References
A branch is a name, not a folder
A branch is a movable reference to a commit. HEAD normally refers to the current branch, and that branch identifies its latest commit. In the clean head lab, main has one commit. Creating idea does not duplicate files or switch branches.
git branch ideagit branch --show-currentgit symbolic-ref --short HEADgit rev-parse main ideaBoth branch names resolve to the same commit ID. The current branch remains main. Conceptual diagram (A is an illustrative commit label): HEAD → main → A, idea → A.
A commit moves only the current branch
Continue the lab. Change the notes file on main and commit it. The idea branch still identifies the original snapshot.
printf "Tea\nMilk\n" > notes.txtgit add -- notes.txtgit commit -m "Add milk"git log --oneline --decorate --allNow HEAD → main → B → A and idea → A; B has A as its parent. The commit IDs depend on the fixture. The working tree is not a separate copy for each branch.
Detached HEAD and preserving a commit
In the same clean fixture, inspect the earlier snapshot by detaching HEAD at idea. This is a valid state, but new commits do not advance a named branch. If you decide to keep new work, create a branch before leaving it.
git switch --detach ideagit branch --show-currentprintf "Tea\nRice\n" > notes.txtgit add -- notes.txtgit commit -m "Try rice"git switch -c experimentgit switch mainThe current-branch query prints nothing when detached. The experiment branch preserves the trial commit; switching back to main restores its tracked snapshot. Do not rely on reflog as a permanent backup.
Practice
Draw A with two branch labels, then advance only one. Predict where HEAD points after switching.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Branches and HEAD: Understand Movable References — knowledge check
Reason about branch references, snapshots and safe local operations.
What does a local branch name identify?
What happens after git branch idea on main?
HEAD refers to main, and main and idea point to A. A new commit B is made. Which ref moves?
What does git branch --show-current print in detached HEAD?
How should you preserve a new detached commit before leaving?