Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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)

Reference: https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-ltbranchgt

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

E.g. 

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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...