Git

Is There Any Way to Git Checkout Previous Branch?

Branches are pointers or labels to a particular commit in Git. While working on large projects, developers create multiple branches and work on a separate branch for each feature. They frequently need to switch between branches for modifications and testing different features. For this corresponding purpose, Git commands are available to switch between branches easily and immediately.

This write-up will illustrate the methods to Git checkout the previous branch.

Is There Any Way to Git Checkout the Previous Branch?

Yes, there are various methods to Git checkout the previous or latest branch. Different Git commands are available to do this, such as:

Method 1: Checkout the Previous Branch Using “git checkout -” Command

To checkout the previous branch, first, lists all the available branches of the current repository. Then, navigate to the desired branch. After that, run the “git checkout” command with the “” symbol or the “@{-1}” option to switch to the previous branch.

Step 1: View Available Branches

First, list all the available branches of the current repository:

git branch

It can be observed that the repository contains three branches, and the “master” branch is the current working branch:

Step 2: Switch to Another Branch

Next, type out the “git switch” command along with the target branch name and switch to it:

git switch alpha

Here, “alpha” is our target branch name and we have switched to it successfully:

Now, suppose that we want to switch to our previous branch. To do this, follow the next provided steps.

Step 3: Checkout Previous Branch

Now, switch to the previous branch by using the “” symbol with the “git switch” command:

git switch -

The below output indicates that we have been switched to the previous “master” branch successfully:

Alternatively, the “@{-1}” option can also be utilized to checkout the previous branch:

git switch @{-1}

As you can see, we have switched from the “master” branch to the “alpha” branch:

Method 2: Checkout the Previous Branch Using “git switch -” Command

The “git switch” command can also be used with the “” symbol or the “@{-1}” option to checkout the previous branch. To do so, follow the given-provided steps.

Step 1: List Available Branches

First, display all the available branches in the current repository:

git branch

According to the below output, the repository contains three branches, and we are currently on the “master” branch:

Step 2: Checkout the Previous Branch

To switch back to the previous branch, execute the below-stated command:

git checkout -

It can be observed that we have checked out to the previous branch which is “alpha”:

Alternatively, the “@{-1}” option can also be utilized with the same command to switch to the previous branch:

git checkout @{-1}

It can be observed that we have switched from the “master” branch to the “alpha” branch:

We have efficiently explained the procedure to switch to the previous branch in Git.

Conclusion

Different Git commands can be used to Git checkout the previous branch, such as “git switch –” and the “git checkout –” commands. Moreover, the “@{-1}” option is utilized with the “git switch” or “git checkout” command to switch back to the previous branch. This write-up explained the methods to Git checkout the previous or old branch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.