Git: How to return from the "disconnected HEAD" state

If you check the branch:

git checkout 760ac7e

eg. b9ac70b , how can one return to the last known head of b9ac70b without knowing its SHA1?

+71
git
Aug 03 '12 at 18:11
source share
3 answers

If you remember which branch was checked before (for example, master ), you could simply

 git checkout master 

to exit the offline state of HEAD.

Generally speaking: git checkout <branchname> will save you from this.

If you don’t remember the name of the last branch, try

 git checkout - 

It is also trying to check your last remote branch.

+123
Aug 03 '12 at 18:20
source share

Use git reflog to find hashes of previously committed commits.

The quick access command to go to your last checked branch (not sure if this work is correct with the detached HEAD and intermediate latch, though) is git checkout -

+13
Aug 03 '12 at 18:13
source share

I had this case with an edge where I checked the previous version of the code in which the structure of my directory was changed:

 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 this case, you may need to use --force (when you know that reverting to the original branch and reverting the changes is a safe thing).

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'. 
0
Apr 10 '17 at 9:41 on
source share



All Articles