git checkout
Move HEAD to another branch and rebuild your working tree.
git checkoutWhat it does
git checkout does two big things: it updates HEAD to point at another branch (or detaches it at a commit), and it rewrites your working directory to match that target. Files are added, removed or modified on disk so your working tree reflects the checked-out state.
The 'HEAD' capsule glides from main to the feature commit — and the working tree rewrites itself
Watch it — press play
A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.
Step 1
Where do you want to stand?
Checkout flips HEAD from one branch to another — and rewrites your working files to match that branch.
git checkout featureHEAD is about to glide to feature
Under the hood
HEAD flips from one ref to another
HEAD is a file. `git checkout main` writes ref: refs/heads/main into it. That single edit changes which branch 'you are on.'
The working tree is synchronized
Git compares the checked-out commit's tree with your current files, then copies the difference onto disk. Blobs are read from objects/ and written out as real files.
Git will block you, not your data
If a file in the way has uncommitted changes, checkout refuses with 'Your local changes would be overwritten.' Git would rather fail loudly than destroy your edits. Add -f only if you truly mean it.
Detached HEAD
`git checkout 74d3b9f` moves HEAD to a raw commit instead of a branch. You're now in a 'detached' state: commits you make belong to no branch and can be swept away by GC — unless you name a branch to hang onto them.
Aliases the pros type
Memorize the git checkout concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:
git config --global alias.st # → git checkoutg cogit checkoutThe legendary alias, carved into a billion muscle memories.
g cobgit checkout -bCreate + switch in one keystroke.
g co -- .git checkout -- .Revert the whole working tree to HEAD. Handle with care.
Command anatomy
-b <name>Create a branch, then check it out — the classic branch+switch.
-- <path>Discard working-tree changes to a specific file only.
-fForce the switch, discarding uncommitted changes.
When to reach for it
Jumping between feature branches to context-switch.
You never use git branch alone — you always want -b to create and switch in one step.
Going back to inspect an old commit directly (careful: detached HEAD).
Pro tip
git checkout does double duty (switch branches AND restore files) which confused generations of users. That split is exactly why git switch and git restore exist now — switch for refs, restore for files.