3

Is there a simple way to switch back to the branch you were most recently on prior to the current one? The same concept as "cd -" from the command line.

When working on multiple branches, this can be useful.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106

2 Answers2

3

Just do:

git checkout -

Note the dash. This checks out the latest branch you were on before the current one.

As you can see, the syntax is analogous to cd -

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • Wow, I didn't give the git dev team enough credit. Thank you! – Sridhar Sarnobat Apr 16 '13 at 17:14
  • Thanks, that's a nice trick. After you mentioned it, I found it buried in the [git-checkout(1)](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) man page. I've posted the quote from the man page as another answer, so it can be formatted correctly. – Trebor Rude Apr 17 '13 at 03:30
1

I never would have found this without the answer from @KlasMellbourn, but this is actually mentioned in the git-checkout(1) man page (in the portion describing <branch>):

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}".
Trebor Rude
  • 1,904
  • 1
  • 21
  • 31