Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (29.3k points)

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 again to return to foo.

Does anything like this exist? Would it be hard to implement?

1 Answer

0 votes
by (50.2k points)


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}".


git branch --track mybranch @{-1}

git merge @{-1}

git rev-parse --symbolic-full-name @{-1} 

It would work as expected.

Note: git checkout - is a shorthand for git checkout @{-1}.

I hope this will help you to checkout the previous branch.

Browse Categories