How to Checkout the Previous Branch in Git

  1. Checkout the previous branch
  2. The git checkout Command

As time is precious, the command line shortcuts are always warmly welcomed by programmers. Git suggests an easy way of switching between the current branch and the previous ones. Here, we are going to discuss how to do that.

Checkout the previous branch

The simplest way of switching to the previous branch is using the git checkout command:

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

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

The git checkout command allows not only switching exactly to the previous branch but, also, getting back to the N-th last branch/commit.

Here is how you can do that:

git checkout @{-N}

You can also git merge the previous branch into the current one by running the following command:

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 specific 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.

The git checkout Command

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