Search code examples
gitgit-checkout

Git pushd & popd? I.e., checkout last state


I'm writing a Bash script, and I want to checkout a tag and then checkout back to where I started.

I tried git co HEAD@{1}, but when starting at master, that takes me back to the commit SHA of master but with a detatched head.

Is there something like pushd & popd for Git?


Solution

  • git checkout @{-1} which can be abbreviated to git checkout -.

    From the manpage:

    As a special case, the "@{-N}" syntax for the N-th last branch checks out the branch (instead of detaching). You may also specify - which is synonymous with "@{-1}".