Git: How to return from 'detached HEAD' state

If one would checkout a branch:

git checkout 760ac7e

from e.g. b9ac70b, how can one go back to the last known head b9ac70b without knowing its SHA1?


If you remember which branch was checked out before (e.g. master) you could simply

git checkout master

to get out of detached HEAD state.

Generally speaking: git checkout <branchname> will get you out of that.

If you don't remember the last branch name, try

git checkout -

This also tries to check out your last checked out branch.


Use git reflog to find the hashes of previously checked out commits.

A shortcut command to get to your last checked out branch (not sure if this work correctly with detached HEAD and intermediate commits though) is git checkout -