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.
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).
Practice
A real repo, pre-seeded with history, runs right here — stage, commit, branch, stash, diff, even hit and resolve a real merge conflict. Nothing to install, nothing leaves your machine.
Open practice playground