Git Checkout: Switch Branches, Files, and Commits
git checkout is one of Git's most versatile commands. This guide covers switching branches, checking out specific commits (detached HEAD), restoring individual files, and how the newer git switch and git restore commands relate to it.
Beginner9 min readUpdated
What is git checkout?
git checkout switches branches, restores files, or navigates to a specific commit. Modern Git (2.23+) splits this into two focused commands: git switch for branches and git restore for files.
The short version
- 1Switch branch:
git switch <branch>orgit checkout <branch> - 2Create and switch:
git switch -c <new-branch> - 3Check out a commit:
git checkout <sha>(enters detached HEAD) - 4Restore a file:
git restore <file> - 5Unstage a file:
git restore --staged <file>
| Aspect | git checkout | git switch / git restore |
|---|---|---|
| Switch branches | git checkout <branch> | git switch <branch> |
| Create + switch | git checkout -b <branch> | git switch -c <branch> |
| Discard file changes | git checkout -- <file> | git restore <file> |
| Unstage a file | git reset HEAD <file> | git restore --staged <file> |
| Restore from commit | git checkout <sha> -- <file> | git restore --source <sha> <file> |
Why it matters
You run git checkout to switch branches and it works, but then you use it to look at an old commit and Git tells you you're in a 'detached HEAD state'. What does that even mean?
git checkout is one of Git's oldest and most overloaded commands. It can switch branches, restore files, and navigate to specific commits. Understanding what it does in each case, and when to use the newer git switch and git restore instead, will save you from confusing mistakes.
Where it usually goes wrong
- Ending up in a detached HEAD state and making commits that get lost
- Using
git checkout <file>to discard changes and permanently losing work - Forgetting to create a branch after checking out a specific commit
- Confusion between
git checkout branchandgit checkout -- file
Git checkout, step by step
Step 1: git checkout vs git switch vs git restore: what's the difference?
Git 2.23 (2019) introduced
git switchandgit restoreto splitgit checkoutinto two focused commands. All three still work today, but the newer commands are clearer and harder to misuse.Terminal# git checkout does THREE different things (confusing):git checkout feature/login # switches branchgit checkout -b new-branch # creates + switches branchgit checkout -- src/app.ts # discards file changes (DANGEROUS, easy to mistype)# git switch - branch operations only (clear intent):git switch feature/login # switches branchgit switch -c new-branch # creates + switches branch# git restore - file operations only (clear intent):git restore src/app.ts # discards file changes in working dirgit restore --staged src/app.ts # unstages a file (keeps changes in working dir)git restore --source HEAD~2 src/app.ts # restore file from 2 commits agoTip: Use
git switchandgit restorefor new work: they make intent explicit and prevent the accidental file-destruction bug ofgit checkout -- file. The oldgit checkoutstill works and won't be removed.Step 2: Switch to an existing branch
The most common use. Both syntaxes work;
git switchis clearer.Terminal# Modern (recommended)git switch feature/login# Classicgit checkout feature/loginStep 3: Create and switch to a new branch
Create a branch and check it out in one step.
Terminal# Moderngit switch -c feature/new-thing# Classicgit checkout -b feature/new-thingStep 4: Check out a specific commit (detached HEAD)
You can navigate to any commit by its SHA. Git enters 'detached HEAD' mode: you're not on any branch.
Terminalgit checkout a3f2c1d# Git warns you:# HEAD is now at a3f2c1d add login form# You are in 'detached HEAD' state.Careful: In detached HEAD state, any commits you make won't belong to a branch. If you switch away, those commits become unreachable. Create a branch immediately if you want to keep them:
git switch -c my-branch.In Gitoryx: Double-click any branch or commit in the graph to check it outHow it works Step 5: Check out a remote branch locally
To start working on a branch that exists only on the remote, create a local tracking branch.
Terminalgit switch -c feature/remote-work origin/feature/remote-work# or shorthand:git checkout --track origin/feature/remote-workStep 6: Restore a file to its last committed state
Discard all changes to a specific file and revert it to the last commit.
git restoreis the clear, modern way to do this.Terminal# Modern (recommended - explicit, no ambiguity)git restore src/app.ts# Unstage a file (keep changes in working dir, just remove from staging)git restore --staged src/app.ts# Classic (still works but ambiguous syntax - easy to accidentally destroy work)git checkout -- src/app.tsCareful: This permanently discards your uncommitted changes to that file. There is no undo.
Step 7: Restore a file from a specific commit
Pull a specific version of a file from any commit into your working directory.
Terminalgit restore --source a3f2c1d src/app.ts# or classic:git checkout a3f2c1d -- src/app.tsTip: This doesn't change your branch: it just replaces the file content with the version from that commit.
Mistakes to avoid with git checkout
- Making commits in detached HEAD state
- Commits made in detached HEAD are not reachable by any branch. When you switch branches, Git warns you they'll be lost.
- Fix: As soon as you realize you're in detached HEAD and want to keep your work, run
git switch -c <new-branch>to attach your commits to a real branch. - Using `git checkout -- <file>` accidentally
- It looks similar to other checkout commands but discards all your uncommitted file changes permanently.
- Fix: Prefer
git restore <file>: it's explicit about restoring files and harder to mistype. Always rungit statusbefore restoring to confirm you're targeting the right file. - Not knowing the difference between `git switch` and `git checkout`
git checkoutdoes too many things;git switchandgit restorewere introduced in Git 2.23 to make the intent clear.- Fix: Use
git switchfor branch operations andgit restorefor file operations going forward.
Git checkout in Gitoryx
In Gitoryx, double-clicking a branch or commit in the graph checks it out instantly, with a clear visual indicator when you're in detached HEAD state.
- Double-click any branch or commit in the graph to check it out
- Detached HEAD state is clearly indicated with a special icon
- Create a branch from a detached HEAD in one click
- Restore individual files with right-click → Discard changes
Free plan · no account required · macOS, Windows and Linux
Git checkout: frequently asked questions
- What is the difference between git checkout, git switch, and git restore?
git checkoutdoes three things at once: switch branches, create branches, and restore files, which makes it error-prone. Git 2.23 split it into two focused commands:git switchfor branch operations (switch, create) andgit restorefor file operations (discard changes, unstage). All three still work, butgit switchandgit restoreare clearer and safer for new work.- What is detached HEAD in Git?
- Detached HEAD means your HEAD pointer points directly to a commit instead of a branch. You can browse and even commit, but those commits won't belong to any branch and will be lost if you check out another branch without creating a new one first.
- How do I get out of detached HEAD state?
- Run
git switch <branch-name>to return to a named branch. If you made commits you want to keep, first rungit switch -c <new-branch>to save them on a new branch before switching away. - How do I unstage a file in Git?
- Use
git restore --staged <file>to unstage a file. This removes it from the staging area but keeps your changes in the working directory. The classic equivalent isgit reset HEAD <file>. - Can I restore a deleted file with git checkout?
- Yes. Use
git restore <file>if the file was deleted since the last commit, orgit restore --source <sha> <file>to restore a file from a specific commit in history. - What is the difference between `git checkout` and `git switch`?
git switchwas introduced in Git 2.23 as a focused replacement for the branch-switching part ofgit checkout. Similarly,git restorehandles file restoration. The oldgit checkoutstill works but does too many different things.- Can I check out a file without switching branches?
- Yes.
git restore <file>restores a file to its last committed state without changing your current branch. Usegit restore --source <sha> <file>to restore from a specific commit.
Related reading
- How to Undo a Commit in Git (The Right Way)Tutorial · 10 min
- Git Reset: Undo Changes at Any StageTutorial · 10 min