How to Go Back to the Previous Branch with git switch –
Running git switch - takes you back to the branch you were on before. It saves you from retyping branch names when you move back and forth between a working branch and main many times, or when you switch to the wrong branch by mistake. I also checked, by looking at the output before and after each operation in a test environment, how to go back two branches, the situations where going back fails with an error, and how to refer to the "previous branch" with other commands such as git merge -.
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>). git switch was added in Git 2.23.0, so with an older Git use git checkout -. I did not test git cherry-pick - or git rebase -, nor Git on Windows or macOS.
- 1. The repository before the operations
- 2. Going back to the branch you switched from with git switch –
- 3. The previous branch is determined by the switch history
- 4. Going back two branches with @{-2}
- 5. Cases where the destination changes depending on how you switched
- 6. When going back fails with an error
- 7. When you have uncommitted changes
- 8. Referring to the "previous branch" in other commands
The repository before the operations
I tested in a repository with three branches: main, sale, and fix-typo. Only the sale branch has a commit that sets the shipping fee to 300 yen. I am currently on main.
$ git branch
fix-typo
* main
sale
$ git log --all --format="%h %an <%ae> %s"
8b0abe8 Neko <neko@example.com> セールで送料を300円に
ae84adb Neko <neko@example.com> 最初のコミット
$ cat price.txt
送料: 500円
Going back to the branch you switched from with git switch –
I switched to sale and then ran git switch -, and it went back to main. The * in git branch moved to main, and price.txt now has the content from main (500 yen).
$ git switch sale
Switched to branch 'sale'
$ cat price.txt
送料: 300円
$ git switch -
Switched to branch 'main'
$ git branch
fix-typo
* main
sale
$ cat price.txt
送料: 500円
When I ran git switch - again, it went back to sale. Each time I repeated it, it alternated between the two branches.
$ git switch -
Switched to branch 'sale'
$ git branch --show-current
sale
$ git switch -
Switched to branch 'main'
$ git branch --show-current
main
The official reference for git switch says that - has the same meaning as @{-1}. It also says this is useful for quickly switching back and forth between two branches, or for undoing a switch you made by mistake. The explanation that - means @{-1} also appears in the official reference for git checkout, where git checkout - can also point to the previous branch. The difference between git switch and git checkout is covered in a separate article, "The Difference Between git switch and checkout, and How to Switch Branches."
The previous branch is determined by the switch history
Each time Git switches branches, it records a line in the reflog of HEAD, such as "checkout: moving from 〇〇 to △△". Even when you switch with git switch, the recorded text says "checkout". The history of the operations so far was as follows.
$ git reflog -5
ae84adb HEAD@{0}: checkout: moving from sale to main
8b0abe8 HEAD@{1}: checkout: moving from main to sale
ae84adb HEAD@{2}: checkout: moving from sale to main
8b0abe8 HEAD@{3}: checkout: moving from main to sale
ae84adb HEAD@{4}: checkout: moving from sale to main
You can check which branch @{-1} refers to with git rev-parse --symbolic-full-name. In the most recent record, sale appeared on the "from" side.
$ git rev-parse --symbolic-full-name @{-1}
refs/heads/sale
Going back two branches with @{-2}
Increasing N in @{-N} lets you point further back. The Git documentation on revision syntax (gitrevisions) says that @{-<n>} is the branch (or commit) you switched away from n switches ago. I tested by switching from main to fix-typo, then from fix-typo to sale.
$ git switch fix-typo
Switched to branch 'fix-typo'
$ git switch sale
Switched to branch 'sale'
$ git reflog -3 --format="%gs"
checkout: moving from fix-typo to sale
checkout: moving from main to fix-typo
checkout: moving from sale to main
$ git rev-parse --symbolic-full-name @{-1} @{-2} @{-3}
refs/heads/fix-typo
refs/heads/main
refs/heads/sale

While on sale, I ran git switch @{-2}, and it skipped fix-typo and went back to main. The following git switch - returned to sale, the branch I was on before moving with @{-2}.
$ git switch @{-2}
Switched to branch 'main'
$ git branch --show-current
main
$ git switch -
Switched to branch 'sale'
$ git branch --show-current
sale
Cases where the destination changes depending on how you switched
git switch - follows the switch history, so the branch it returns to and the result depend on how you switched last.
When you specify the branch you are already on
When I ran git switch main while on main, it displayed "Already on 'main'" and nothing changed. However, the reflog still records "moving from main to main". As a result, the previous branch also becomes main, and git switch - could no longer return to sale.
$ git switch sale && git switch main
Switched to branch 'sale'
Switched to branch 'main'
$ git switch main
Already on 'main'
$ git reflog -1 --format="%gs"
checkout: moving from main to main
$ git rev-parse --symbolic-full-name @{-1}
refs/heads/main
$ git switch -
Already on 'main'
In this state, if you want to return to sale, specify the branch name, as in git switch sale.
When you created a branch and switched to it
Even after creating a new branch and switching to it with git switch -c, git switch - returned me to the branch I was on before creating it. Running it again took me back to the branch I created.
$ git switch -c campaign
Switched to a new branch 'campaign'
$ git switch -
Switched to branch 'main'
$ git switch -
Switched to branch 'campaign'
When the previous position was a commit, not a branch
After I used git switch --detach to view a commit directly (a detached HEAD) and then went back to main, running git switch - produced an error. The previous position was a commit rather than a branch, so git switch stopped and asked for a branch. Following the hint and adding --detach took me back to that commit.
$ git switch --detach HEAD
HEAD is now at ae84adb 最初のコミット
$ git switch -
Switched to branch 'main'
$ git switch -
fatal: a branch is expected, got commit 'ae84adbcb08e964e1d292cf2381d9ca0fbd53182'
hint: If you want to detach HEAD at the commit, try again with the --detach option.
$ git switch --detach -
HEAD is now at ae84adb 最初のコミット
$ git switch -
Switched to branch 'main'
When going back fails with an error
git switch - can stop with fatal: invalid reference: @{-1}. In the test environment, this appeared in the following two situations.
The first was after deleting the branch you were on before. I went from fix-typo back to main, deleted fix-typo, and then ran git switch -.
$ git switch fix-typo && git switch main
Switched to branch 'fix-typo'
Switched to branch 'main'
$ git branch -d fix-typo
Deleted branch fix-typo (was ae84adb).
$ git switch -
fatal: invalid reference: @{-1}
The second was in a repository where you have not switched branches even once. The same error appeared right after running git init in an empty folder, and also after making the first commit.
$ git init -q
$ git switch -
fatal: invalid reference: @{-1}
$ git commit -q --allow-empty -m "最初のコミット" && git switch -
fatal: invalid reference: @{-1}
When you have uncommitted changes
git switch - also stops without switching when it would overwrite your local changes. I tested this after returning to main with the previous branch set to sale. I changed price.txt on main and then ran git switch -, which stopped with the following error.
$ echo "送料: 0円" > price.txt && git status --short
M price.txt
$ git switch -
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
If you want to switch without losing your local changes, commit them first or set them aside with git stash, as the error message suggests.
Referring to the "previous branch" in other commands
@{-1} also works in commands other than git switch. The Git 1.6.2 release notes say that it is accepted not only where a commit is specified but also where a branch name is written, and is treated the same as typing that branch name. While on main with the previous branch set to sale, I checked the diff and the list of commits.
$ git branch --show-current; git rev-parse --symbolic-full-name @{-1}
main
refs/heads/sale
$ git diff --stat @{-1}
price.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log --oneline main..@{-1}
8b0abe8 セールで送料を300円に
Some commands also accept - alone to mean the previous branch. According to the Git release notes, git cherry-pick - (1.8.5) and git rebase - (2.0.0) were added in those respective versions. git merge - was added in 1.7.6.
In the test environment, I tried git merge - by going back to main and then bringing in the change I had checked on sale. The sale commit went into main, and price.txt became 300 yen.
$ git merge -
Updating ae84adb..8b0abe8
Fast-forward
price.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log --oneline
8b0abe8 セールで送料を300円に
ae84adb 最初のコミット
$ cat price.txt
送料: 300円




