git checkout / git switch
Moving HEAD Around
Switching branches does exactly two things: it rewrites .git/HEADto point at the new branch, then it updates your working directory to match that branch's commit tree. No magic — just a file write and a file sync.
Watch HEAD and the Working Directory
Click the buttons to switch branches. Watch both panels change in lockstep — the .git/HEAD file on the left, and the working directory on the right.
.git/HEAD
Working Directory
What Git Actually Does
When you run git switch feature, Git performs three steps in order:
- 1Resolve the branch name to its commit hash by reading
.git/refs/heads/feature. - 2Walk that commit's tree and update every file in the working directory to match. Files unique to the new branch appear; files that no longer exist are removed.
- 3Overwrite
.git/HEADwithref: refs/heads/feature.
checkout vs switch
git checkout is the old Swiss-army-knife command. It could switch branches, restore files, detach HEAD, and more — all with the same command. This was confusing.
Git 2.23 (2019) split it into two focused commands:
git switch <branch>— move HEAD to a branchgit switch -c <branch>— create and switchgit restore <file>— discard working directory changes
Both checkout and switch work today. Prefer switchin new scripts — it's clearer about intent.
When Git Refuses to Switch
Git will block a switch if you have uncommitted changes that would be overwritten by the new branch's tree. It is protecting you from losing work.