How to Checkout the Previous Branch

As time is precious, the command line shortcuts are always warmly welcomed by programmers. Git suggests an easy way of switching between the current git branch and the previous one, which we are going to discuss in the scope of this tutorial.

The simplest way of switching to the previous branch is:

git checkout @{-N}

The @{-N} syntax refers to the N-th last branch/commit checked out using the git checkout operation.

The Git 1.6.2 version suggests an alias of the given command:

git checkout -
git checkout - is equivalent to git checkout @{-1}

This is similar to cd - command in Linux, which switches the current directory to the previous one.

You can also use the same logic to git merge the previous branch into the current one by running:

git merge -
There is also another command suggested by Git version 2.24.1, which is experimental, and the behavior may change:
git switch -

The git switch command switches to a specified branch. The working tree and the staging area are updated to match the branch, and all new git commits will be added to the tip of that branch.

Checking out the Previous Branch

It is a very common thing to move back and forth between two branches. Git has simplified programmers' work, and now there is no need to write the branch name each time. This happens due to the git checkout command, which switches branches or restores working tree files. It allows switching between several features in just a single Git Repository.


Do you find this helpful?

Related articles