Git – Checkout Previous Branch

While working with Git, it’s very common to move back and forth between two branches.

If you want to checkout the previous branch you was working on, there is no need to type the name of this branch each time.

In this short note i’ll show the easy way to switch between two branches in Git.

Git – Checkout Previous Branch

Checkout the previous branch:

$ git checkout -

This behavior is similar to cd - command in Linux, that changes the current directory to the previous one.

If you create an alias for git checkout it will be really handy to use git co - to checkout the previous branch:

$ git config --global alias.co checkout
$ git co -

This also works with other commands like git merge, that is very useful when you want to merge the branch that you were just working on into the current branch.

Merge the previous branch into the current one:

$ git merge -

One Reply to “Git – Checkout Previous Branch”

  1. Check out https://github.com/kaunteya/git-checkout-interactive. It is a git extension that I have written which solves this problem

Leave a Reply