Progress:
TIER 1 · MODULE 15· Basics

git checkout

The original swiss-army command. Use switch/restore instead today.

🎯 What & why

The original swiss-army command for switching branches and restoring files. Still works; still ships. New code should use git switch (branches) and git restore (files) instead — the split makes intent unambiguous.

🧠 Mental model

Two unrelated jobs in one verb: 'change which branch HEAD points to' and 'overwrite paths in the working tree from a tree-ish.' Whether checkout does one or the other depends on its arguments. That overload is exactly why switch/restore were created.

🛠️ Synopsis

git checkout [<options>] <branch>
git checkout [<options>] [<branch>] -- <pathspec>...
git checkout [-b | -B] <new-branch> [<start-point>]

🎚️ Switches & options

FlagWhat it does
-b <name>Create a new branch and check it out.
-B <name>Like -b but reset if it already exists.
--detachCheck out a commit without making/moving a branch — detached HEAD.
-- <path>Restore <path> from the index (or from <branch>:<path> if a tree-ish is given).
--orphan <name>New branch with no history — empty parent.

💡 Use cases

🧪 Examples

Switch to an existing branch.
$ git checkout feature/login
Create and switch to a new branch.
$ git checkout -b feature/login
Restore a single file from HEAD (legacy form of git restore).
$ git checkout -- src/main.c
Detached HEAD at a specific commit (good for inspection, bad for committing).
$ git checkout --detach v1.2

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.