The easiest way to return to the last Git branch

I don’t know about you, but it’s common for me to switch between branches in Git.  After all, that’s one of the main advantages of using Git — the incredible ease with which you can create and merge branches.

Just a few minutes ago, I was in the “adwords” branch of an application I’m working on.  I wanted to go back to “master”, make sure that I hadn’t missed any commits from the central repository, and then go back to “adwords”.  If there were any commits in “master” that I was missing in “adwords”, I figured that I would just rebase from “master” to “adwords”.

So I did a “git checkout master”, and found that I was up to date with the central Git server. For reasons that I can’t explain, I then decided to try something out: Instead of returning to the previous branch with “git checkout adwords”, I instead typed

git checkout -

(That’s a minus sign after the word “checkout.”)

Sure enough, I returned to the “adwords” branch!

Now, there is a fair amount of logic to this: In the Unix shell, you can typically return to the previous directory with “cd -“, which has proven to be quite useful over the years.  In Git, of course, branches are just aliases to commits.  So “git checkout -” is returning you to the previous branch, but it’s really just taking you back to whatever the last commit was that you worked on.

I just checked the Git documentation (“git checkout –help”), and it seems that this is a special case of a more generalizable command structure:

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

I can’t imagine wanting to tell Git to return to the 5th-most-recent branch that I worked on, so this generalized formula seems a bit much to me.

I predict that this trick will save me precious seconds every day, all of which I squandered in writing this blog post.  But I do think that this is a super-cool trick and feature, and demonstrates once again how clever and useful Git is.

Update: “git switch”

Git 2.23, released in 2019, introduced “git switch” as a dedicated command for moving between branches. It exists because “git checkout” had grown to do rather a lot of unrelated jobs, and splitting them apart made each one easier to reason about. The trick in this post works there too:

git switch -

Same minus sign, same result: back to the branch you were just on.

“git checkout -” still works, and I doubt it is going anywhere. But “git switch” says what it means, and it cannot quietly discard your changes to a file the way “git checkout” can — restoring files is now “git restore”, a third command with its own name. If you are teaching Git to someone, that separation is worth a lot.

Want to practice? The LernerPython practice system has two Git exercises in its interactive demo. They accept either command — and if you type “git checkout”, the system suggests “git switch” instead, one of the ways it nudges you toward how Git is written today rather than how it was written when I first noticed this trick in 2014.