0 votes
1 view
in Devops and Agile by (25.5k 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 (42.7k 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 !


Categories

...