Published on

Git Tip: Checkout Previous or Last Branch Instantly

Authors

When working on a project, we might switch between branches (like master and your feature branch) in git very often.

Here's how you can do it without explicitly mentioning the branch name

Just run the following command (note: - at the end of the checkout)

git checkout -

Too lazy to type git checkout? Well, you can set an alias like this in your bash/zsh file like this

alias gc="git checkout"

And you can use the short command like this instead

gc -

How does it work?

It's a shorthand for git checkout @{-1}. -1 implies that it's the previous branch.

Happy switching branches!