Search code examples
gitversion-control

How do I get my 'Detached HEAD' commits back into master


Looks like a few days ago I created a branch called detached HEAD and have been committing to it. My normal process is to commit to master and then push that to origin. But I can't push detached HEAD.

My next stop screwed me. I selected git checkout master - and my detached HEAD branch disappeared. Going back to my project all of my changes in the past few days have been wiped.

Is there anyway I can get those changes back?


Solution

  • If checkout master was the last thing you did, then the reflog entry HEAD@{1} will contain your commits (otherwise use git reflog or git log -p to find them). Use git merge HEAD@{1} to fast forward them into master.

    As noted in the comments, Git Ready has a great article on this.

    git reflog and git reflog --all will give you the commit hashes of the mis-placed commits.

    Git Ready: Reflog, Your Safety Net

    Source: http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html