Is there any way to git checkout previous branch?

From the release notes for 1.6.2

@{-1} is a way to refer to the last branch you were on. This is
accepted not only where an object name is expected, but anywhere a branch name is expected and acts as if you typed the branch name.
E.g. git branch --track mybranch @{-1}, git merge @{-1}, and
git rev-parse --symbolic-full-name @{-1} would work as expected.

and

git checkout - is a shorthand for git checkout @{-1}.


The simplest way of doing this nowadays is:

git checkout -

... which is an alias of:

git checkout @{-1}

git checkout minus

If you want to know more about this, I wrote an entire article about it here: Checkout The Previous Branch In Git.


Git version 2.23 introduced the git switch command which you can use to do that (and more). Quoting the official documentation:

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

In your specific case you can issue git switch - to go back to the branch you were previously on. You can execute the same command again to return to the first branch.

P.S. It's not that you don't already know how to do that (I mean it's been 7 years since you asked this question), but this command is less confusing and friendly to beginners as it addresses a common confusion that arises when using git checkout.