Last Updated: February 25, 2016
· j1mr10rd4n

Quickly switch between last-used branches in git

Much like the cd shell command allows you to change back to the previous directory you were in, git checkout allows you to switch back to the previous branch you were on by passing it a single - argument.

Have a look at the following git session, starting on my_feature_branch (I'm also using the co shortcut for checkout):

~/Development/project (my_feature_branch ✔) ᐅ git co develop
Switched to branch 'develop'
~/Development/project (develop ✔) ᐅ git co -
Switched to branch 'my_feature_branch'
~/Development/project (jobs_4287_part2 ✔) ᐅ git co -
Switched to branch 'develop'

From the git documentation:

As a special case, the "@{-N}" syntax for the N-th last branch checks out the branch (instead of detaching).
You may also specify - which is synonymous with "@{-1}".


git checkout @{-3}

will checkout the third-last branch in your branch history.