Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...