Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT restore last detached HEAD

Tags:

git

I have a big problem in my project: this is the scenario. I have an xcode project under Git. Today I realized that the last commit broke some tests, so i checked out the previous commit. I used SourceTree and this is the warning

Doing so will make your working copy a 'detached HEAD', which means you won't be on a branch anymore. If you want to commit after this you'll probably want to either checkout a branch again, or create a new branch. Is this ok?

I worked for an entire day and at the end I committed everything. So I needed to merge my work on develop branch so I checkout the develop branch and... my work instantly disappeared :(

I know it was wrong to detach my HEAD and Sourcetree warned me... but there is a way to restore my work?

like image 806
IgnazioC Avatar asked Feb 07 '13 17:02

IgnazioC


People also ask

How do I get my detached head back?

Note that once Git prunes your detached HEAD state commits, there is no way to get them back. However, if they have not been deleted, you can check out to that SHA-1 commit hash, create a branch, and merge it to the desired branch to preserve the changes.

How do I resolve a detached head in git?

If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.

How do I fix the detached head at origin master?

All you have to do is 'git checkout [branch-name]' where [branch-name] is the name of the original branch from which you got into a detached head state. The (detached from asdfasdf) will disappear. Show activity on this post. And head is re attached!

How do you go back to a previous commit on git?

To view the previous commits, use the git log –-oneline command. This provides the commit details.


3 Answers

If you type git reflog, it will show you the history of what revisions HEAD pointed to. Your detached head should be in there. Once you find it, do git checkout -b my-new-branch abc123 or git branch my-new-branch abc123 (where abc123 is the SHA-1 of the detached HEAD) to create a new branch that points to your detached head. Now you can merge that branch at your leisure.

Generally, if you check out a branch after working on a detached head, Git should tell you the commit from the detached head you had been on, so you can recover it if you need. I've never used SourceTree, so I don't know if it relays that message. But if it did display that message, then you should be able to use that to find the commit, and again use git checkout -b or git branch to create a branch from that commit.

like image 66
Brian Campbell Avatar answered Oct 09 '22 22:10

Brian Campbell


In Sourcetree, you can do this using the GUI.

First find the "lost" commit by looking for a message in the Command History (view:Show Command Output). It will probably be in the command "Switching Branch" after the commit that you lost. In that message, hopefully you'll see the commit comment with a 1234567 commit ID.

Take that Commit ID to next step.

Hit the "Branch" button in the top toolbar and you should get a dialog "New Branch" where you can specify a certain commit. Put that Commit ID in there, specify a new branch name, hit Create Branch and you should get a new branch with your lost commit!

enter image description here

like image 12
blalond Avatar answered Oct 09 '22 23:10

blalond


If you dont want to keep changes of detached HEAD and want to go to latest branch commit use below command directly.

git checkout - 

Note:I will delete all your changes in the detached HEAD.

like image 6
Desert Rose Avatar answered Oct 09 '22 22:10

Desert Rose