git checkout vs. git switch: What's the Difference?
Both git checkout and git switch can move you between Git branches. The difference is that checkout is an older, multi-purpose command that also works with files and commits, while switch is dedicated to branch navigation. For modern Git, use switch for branches and restore for files.
Quick comparison
| Task | git switch | git checkout |
|---|---|---|
| Change branch | git switch branch | git checkout branch |
| Create and change branch | git switch -c branch | git checkout -b branch |
| Restore a file | Not supported | Supported, but git restore is clearer |
| Check out a commit directly | git switch --detach commit | git checkout commit |
git switch and git restore were introduced in Git 2.23. If you use an older Git version, git checkout remains the option for these operations.
Use git switch to change branches
To move to an existing branch, use:
git switch featureTo create a branch and switch to it in one step, use:
git switch -c featureThe explicit -c option means “create a branch,” which can be easier to recognize than checkout -b. To return to the branch you were on previously, run git switch -. Git may refuse to switch if doing so would overwrite uncommitted changes; commit, stash, or otherwise protect those changes first.
The equivalent git checkout commands
The traditional commands for the same branch operations are:
git checkout feature
git checkout -b featureThese still work and appear in older tutorials and scripts. You do not need to avoid them, but git switchmakes it clearer that you are moving between branches and reduces confusion with checkout's file-restoration behavior.
Use git restore for file changes
Since git checkout has historically been used to switch branches and restore files, it can be unclear which action a command will perform. Modern Git has a separate command for restoring file contents. For example, to discard unstaged edits to one file and restore it from HEAD, use:
git restore --source=HEAD --worktree -- path/to/fileThis replaces that file in the working tree with the committed version, so any unstaged edits to it are discarded. Review the command and path before running it. The older form git checkout -- path/to/file can also overwrite working-tree changes; it is not a harmless branch switch.
To unstage changes without discarding them, use git restore --staged -- path/to/file. Read how to undo git add for more ways to unstage files safely.
Check out a commit without switching branches
You can inspect a specific commit in detached HEAD state with either command:
git switch --detach <commit-id>
git checkout <commit-id>Detached HEAD is useful for inspection or testing. New commits made in this state are not on a branch unless you create one. If you want to keep work started there, create a branch before switching away. Learn more in the guide to detached HEAD.
Which command should you use?
- Use
git switchto change or create branches. - Use
git restorewhen you mean to restore file contents or unstage files. - Use
git checkoutwhen maintaining older scripts or following instructions that require it, and understand whether the command targets a branch, commit, or path.
Separating branch navigation from file restoration makes commands easier to read and helps avoid accidentally overwriting work.