Git checkout previous branch

Git checkout previous branch
Photo by Marija Zaric / Unsplash

I often use git checkout - to checkout the previous branch I was working on, which is fine as long as I haven't checkout more than 1 other branch.

Until I get into the habit of using git worktrees I needed a way to easily switch back to the right branch... bit of googling later and I found that there is a git command to do exactly that...

git checkout '@{-2}' (on windows) or git checkout @{-2} everywhere else

Turns out git checkout - is a shorthand for git checkout @{-1}.

From the git documentation...

You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using "git checkout" operation. You may also specify - which is synonymous to @{-1}.
Is there any way to git checkout previous branch?
I sort of want the equivalent of cd - for git. If I am in branch master and I checkout foo, I would love to be able to type something like git checkout - to go back to master, and be able to type it