git switch vs checkout: Differences and How to Switch Branches
If you only want to switch the branch you are working on, git switch and git checkout give the same result. They differ when you pass a commit, when a file has the same name as a branch, and when you mistype a branch name. git switch only accepts branch switching, so it is less likely to do something unintended. I checked this in a test environment, using git branch and git status to confirm how to create a new branch and switch to it, go back to the previous branch, and bring a branch that exists only on the remote into your local repository.
The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). For the remote, I used an empty repository created inside the same environment. git switch was added in Git 2.23.0, so older versions of Git do not have it. I did not test on Windows or macOS, or with remotes on hosting services such as GitHub.
- 1. git checkout's two jobs were split into switch and restore
- 2. Switching to an existing branch
- 3. Going back to the previous branch
- 4. Creating a new branch and switching to it
- 5. When you mistype a branch name
- 6. When a file has the same name as a branch
- 7. Passing a commit to look at an older state
- 8. When you have uncommitted changes
- 9. Switching to a branch that exists only on the remote
- 10. Conversion table
git checkout's two jobs were split into switch and restore
git checkout has two roles: switching branches, and restoring files to discard changes. The one-line description at the top of the git checkout reference is "Switch branches or restore working tree files." According to the Git 2.23.0 release notes, git switch and git restore were added to separate these two roles.

I cover git restore, which restores files, in a separate article, "How to restore deleted files and changes with git restore." This article focuses on switching branches. The examples below were run in a repository with two branches, main and sale.
$ git log --oneline --all
3d1f420 送料を400円に変更
ae84adb 最初のコミット
Switching to an existing branch
In the output of git branch, the branch you are on has a * next to it. When I passed a branch name to git switch, the * moved to sale.
$ git branch
* main
sale
$ git switch sale
Switched to branch 'sale'
$ git branch
main
* sale
$ git status
On branch sale
nothing to commit, working tree clean
Passing a branch name to git checkout also switches branches the same way. The messages shown were the same too.
$ git checkout main
Switched to branch 'main'
$ git branch
* main
sale
Going back to the previous branch
If you pass - instead of a branch name, you return to the branch you were on before. The git switch reference explains that - means the same as @{-1} (the branch you switched to one step earlier), and that it is useful when moving back and forth between two branches or undoing a mistaken switch. git checkout - also returned to the previous branch.
$ git switch -
Switched to branch 'sale'
$ git checkout -
Switched to branch 'main'
$ git branch --show-current
main
Creating a new branch and switching to it
Use -c with git switch and -b with git checkout. Both create a new branch from the commit you are on and switch to it.
$ git switch -c campaign
Switched to a new branch 'campaign'
$ git branch
* campaign
main
sale
$ git switch main
Switched to branch 'main'
$ git checkout -b coupon
Switched to a new branch 'coupon'
$ git branch
campaign
* coupon
main
sale
If you pass a name that already exists, both commands fail.
$ git switch -c campaign
fatal: a branch named 'campaign' already exists
$ git checkout -b campaign
fatal: a branch named 'campaign' already exists
When you mistype a branch name
This is where the two commands start to differ. When I passed a branch name that does not exist (campain, meant to be campaign), the error messages were different.
$ git switch campain
fatal: invalid reference: campain
$ git checkout campain
error: pathspec 'campain' did not match any file(s) known to git
git switch reports that the branch was not found. git checkout, on the other hand, looks for campain as a file name and reports that it did not find one. The usage section of the git checkout reference also lists ways to pass file names (pathspecs) to git checkout.
When a file has the same name as a branch
This treatment as a file name causes problems when a branch and a file have the same name. I created a branch named price.txt, changed the file price.txt, and then ran git checkout price.txt to discard the change.
$ git branch price.txt
$ echo "送料: 0円" > price.txt && git status --short
M price.txt
$ git checkout price.txt
Switched to branch 'price.txt'
M price.txt
$ git branch --show-current; git status --short
price.txt
M price.txt
The change was not discarded, and the command switched to the branch price.txt. The "ARGUMENT DISAMBIGUATION" section of the git checkout reference says that when an argument could be both a branch name and a file name, it is treated as a branch switch. To force it to be treated as a file, put -- before the file name.
$ git switch main
Switched to branch 'main'
M price.txt
$ git checkout -- price.txt
$ git branch --show-current; git status --short; cat price.txt
main
送料: 400円
The usage section of the git switch reference has no form that takes a file name. In the mistyped-name example as well, git switch did not look for a file. It stopped with an error saying the branch was not found.
Passing a commit to look at an older state
Passing a commit instead of a branch also behaves differently. git switch stopped with an error asking for a branch.
$ git switch HEAD~1
fatal: a branch is expected, got commit 'HEAD~1'
hint: If you want to detach HEAD at the commit, try again with the --detach option.
git checkout switched directly to a state where you are not on any branch (detached HEAD).
$ git checkout HEAD~1
Note: switching to 'HEAD~1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at ae84adb 最初のコミット
$ git status | head -1
HEAD detached at ae84adb
According to the message shown, commits you make in this state can be discarded without affecting any branch, by switching back to a branch. If you want to keep them, create a branch with git switch -c <new-branch-name>. To go back, use git switch -.
To get the same state with git switch, add --detach. This makes the detached HEAD explicit with an option. Since you are not on any branch, git branch --show-current prints nothing.
$ git switch -
Previous HEAD position was ae84adb 最初のコミット
Switched to branch 'main'
$ git switch --detach HEAD~1
HEAD is now at ae84adb 最初のコミット
$ git branch --show-current; git status | head -1
HEAD detached at ae84adb
$ git switch main
Previous HEAD position was ae84adb 最初のコミット
Switched to branch 'main'
When you have uncommitted changes
If you have edited a file whose contents differ between the current branch and the target branch, both commands stop the switch. Here, I made a commit on the sale branch that sets the shipping fee to 300 yen, and then edited price.txt on main. The git switch error also says "checkout."
$ echo "送料: 0円" > price.txt && git status --short
M price.txt
$ git switch sale
error: Your local changes to the following files would be overwritten by checkout:
price.txt
Please commit your changes or stash them before you switch branches.
Aborting
$ git checkout sale
error: Your local changes to the following files would be overwritten by checkout:
price.txt
Please commit your changes or stash them before you switch branches.
Aborting
On the other hand, when the change was to a file whose contents are the same on both branches (here, README.md), the switch went through and kept the change. The line starting with M for README.md that appears during the switch shows that the change was carried over.
$ echo "メモ" >> README.md && git restore price.txt && git status --short
M README.md
$ git switch sale
Switched to branch 'sale'
M README.md
$ git status --short; cat README.md
M README.md
# Neko no Te Store
メモ
If you want to switch even though it means discarding local changes, add --discard-changes (or -f). According to the git switch reference, this makes the index and working tree match the contents of the target branch. I went back to main, changed price.txt as well, and tested with changes in both files.
$ git switch main
Switched to branch 'main'
M README.md
$ echo "送料: 0円" > price.txt && git status --short
M README.md
M price.txt
$ git switch --discard-changes sale
Switched to branch 'sale'
$ git status --short; cat price.txt; cat README.md
送料: 300円
# Neko no Te Store
--discard-changes discarded not only the change in price.txt, but also the change in README.md, which could otherwise be carried over. If you want to keep your changes, commit them first or save them with git stash before using this option.
Switching to a branch that exists only on the remote
This is the case where you want to use a branch that someone else created on the remote. After git fetch, git branch -a shows the remote branch remotes/origin/review, but there is no local review branch.
$ git fetch -q origin
$ git branch -a
campaign
coupon
* main
sale
remotes/origin/main
remotes/origin/review
Passing origin/review as is makes git switch stop with an error asking for a branch. If you pass review without origin/, a local review branch is created, and it is also set up to track origin/review.
$ git switch origin/review
fatal: a branch is expected, got remote branch 'origin/review'
hint: If you want to detach HEAD at the commit, try again with the --detach option.
$ git switch review
Switched to a new branch 'review'
branch 'review' set up to track 'origin/review'.
$ git branch -vv
campaign 3d1f420 送料を400円に変更
coupon 3d1f420 送料を400円に変更
main 3d1f420 送料を400円に変更
* review 35047d9 [origin/review] お知らせを追加
sale 0c615e9 セールで送料を300円に
According to the --guess section of the git switch reference, when no local branch has the name you passed but exactly one remote has a branch with that name, the command behaves like git switch -c review --track origin/review. This behavior is enabled by default. I also deleted the local review branch and tried git checkout review, which gave the same result.
$ git checkout review
Switched to a new branch 'review'
branch 'review' set up to track 'origin/review'.
$ git branch -vv
campaign 3d1f420 送料を400円に変更
coupon 3d1f420 送料を400円に変更
main 3d1f420 送料を400円に変更
* review 35047d9 [origin/review] お知らせを追加
sale 0c615e9 セールで送料を300円に
Conversion table
The git checkout commands used for switching branches can be replaced with git switch as shown below. In my test environment, the resulting state after each switch was the same for both commands. The only line where the two differ is passing a commit: git switch needs --detach, and only git checkout prints the long detached HEAD notice.
| What you want to do | git checkout | git switch |
|---|---|---|
| Switch to a branch | git checkout sale |
git switch sale |
| Go back to the previous branch | git checkout - |
git switch - |
| Create a branch and switch to it | git checkout -b campaign |
git switch -c campaign |
| Look at a commit | git checkout HEAD~1 |
git switch --detach HEAD~1 |
| Use a remote branch | git checkout review |
git switch review |
To discard changes to a file, replace git checkout -- <file> with git restore <file> instead of git switch.




