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.

Practice Git workflows →

Quick comparison

Taskgit switchgit checkout
Change branchgit switch branchgit checkout branch
Create and change branchgit switch -c branchgit checkout -b branch
Restore a fileNot supportedSupported, but git restore is clearer
Check out a commit directlygit switch --detach commitgit 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 feature

To create a branch and switch to it in one step, use:

git switch -c feature

The 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 feature

These 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/file

This 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?

Separating branch navigation from file restoration makes commands easier to read and helps avoid accidentally overwriting work.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →