The difference between git checkout and git switch comes down to scope and clarity:
-
git switchis dedicated to branch switching- Introduced in Git 2.23 to make branch operations more intuitive.
- Specifically used to switch branches or create new ones.
- Cannot be used to checkout individual files.
Example usage:
git switch my-branch # Switch to an existing branch git switch -c new-branch # Create and switch to a new branch
-
git checkoutis more general-purpose- Can switch branches or restore individual files.
- Often considered overloaded and confusing.
- Can lead to unintended consequences if used without care.
Example usage:
git checkout my-branch # Switch to an existing branch git checkout -- file.txt # Restore file.txt to its last committed state
- Use
git switchwhen you only need to change branches. - Use
git checkoutif you need to restore files or if using an older Git version.
Sure! Let’s break it down further with edge cases and how to create a new branch using both commands.
| Action | git switch |
git checkout |
Notes |
|---|---|---|---|
| Switch to an existing branch | git switch my-branch |
git checkout my-branch |
Both work the same. |
| Create a new branch and switch to it | git switch -c new-branch |
git checkout -b new-branch |
Both create a branch and switch to it. |
| Switch to a branch with uncommitted changes (conflict-free) | ✅ Allowed | ✅ Allowed | Won't discard changes if they don’t conflict. |
| Switch to a branch with conflicting uncommitted changes | ❌ Prevented (unless using -f) |
❌ Prevented (unless using --force) |
Use -f or --force to override. |
| Restore a file to its last committed state | ❌ Not supported | ✅ git checkout -- file.txt |
git switch does NOT support file-level operations. Use git restore instead. |
| Checkout a specific commit (detached HEAD) | ❌ Not supported | ✅ git checkout <commit> |
Use git switch --detach <commit> instead. |
| Switch to a remote-tracking branch | git switch my-branch (if tracking is set) |
git checkout my-branch |
git switch requires upstream tracking or -c to create it. |
| Detach HEAD (checkout a commit without a branch) | git switch --detach <commit> |
git checkout <commit> |
git switch --detach is the recommended way. |
git switch -c new-branch- The
-cflag stands for "create". - Creates
new-branchand switches to it.
git checkout -b new-branch- The
-bflag stands for "branch". - Does the same as
git switch -c, butgit switchis more explicit.
- A detached HEAD means Git is not on any branch, just at a specific commit.
- This is useful for checking out an old version of the code without modifying branches.
git switch --detach <commit-hash>git checkout <commit-hash>- Both will place you in a detached HEAD state.
- To return to a branch, use
git switch <branch>orgit checkout <branch>.
If you want to revert a file to the last committed version:
git restore file.txtgit checkout -- file.txtgit restoreis the modern equivalent of usingcheckoutfor files.
- Use
git switchwhen changing branches—it’s safer and clearer. - Use
git restoreinstead ofgit checkoutfor files. - Use
git checkoutonly if necessary, like checking out commits or working with older Git versions.
- If you’re switching branches, use
git switch! - If you’re restoring files, use
git restore. - If you’re working with commits,
git checkout(orgit switch --detach) is fine.