All commands

git checkout

Move HEAD to another branch and rebuild your working tree.

What 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.

9a2f1c0init projectfeature4b7c2a5add components74d3b9ffeat: dark modemainHEAD

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.

1 / 3

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 feature
9a2f1c0init project4b7c2a5add componentsc86d9eestyle layoutfeature74d3b9ffeat: dark modemainHEAD

HEAD is about to glide to feature

Under the hood

1

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.'

2

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.

3

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.

4

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 checkout
AliasWhy people use it
g co

The legendary alias, carved into a billion muscle memories.

g cob

Create + switch in one keystroke.

g co -- .

Revert the whole working tree to HEAD. Handle with care.

Command anatomy

git checkout <branch|commit>
-b <name>

Create a branch, then check it out — the classic branch+switch.

-- <path>

Discard working-tree changes to a specific file only.

-f

Force 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.

Go deeper with