Git: How to return from 'detached HEAD' state

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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 -

Solution 3 - Git

I had this edge case, where I checked out a previous version of the code in which my file directory structure was different:

git checkout 1.87.1                                    
warning: unable to unlink web/sites/default/default.settings.php: Permission denied
... other warnings ...
Note: checking out '1.87.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. 
Example:

  git checkout -b <new-branch-name>

HEAD is now at 50a7153d7... Merge branch 'hotfix/1.87.1'

In a case like this you may need to use --force (when you know that going back to the original branch and discarding changes is a safe thing to do).

git checkout master did not work:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
web/sites/default/default.settings.php
... other files ...

git checkout master --force (or git checkout master -f) worked:

git checkout master -f
Previous HEAD position was 50a7153d7... Merge branch 'hotfix/1.87.1'
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

Solution 4 - Git

You may have made some new commits in the detached HEAD state. I believe if you do as other answers advise:

git checkout master
# or
git checkout -

then you may lose your commits!! Instead, you may want to do this:

# you are currently in detached HEAD state
git checkout -b commits-from-detached-head

and then merge commits-from-detached-head into whatever branch you want, so you don't lose the commits.

Solution 5 - Git

Just in case anyone has the same edge case as me: I have a branch called test and was trying to make a branch called test/my-experimental-feature. That confused git because it thought I was referring to a branch that already exists. I changed it to test--my-experimental-feature and it worked fine.

Solution 6 - Git

In general: git checkout <branch*> (git checkout master is a special case).

*from which we (accidentally) detached a commit (using git checkout <commit_hash>)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames RaitsevView Question on Stackoverflow
Solution 1 - GiteckesView Answer on Stackoverflow
Solution 2 - GitknittlView Answer on Stackoverflow
Solution 3 - GitmcaleaaView Answer on Stackoverflow
Solution 4 - Gituser13087176View Answer on Stackoverflow
Solution 5 - GitstoebeljView Answer on Stackoverflow
Solution 6 - GitmirekphdView Answer on Stackoverflow