253

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?

3

2 Answers 2

485

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

6
  • 11
    Not being a power git user this was helpful, but not details enough. It prompted me to search the net though - thanks. I found gitready.com/intermediate/2009/02/09/… very useful
    – rgardler
    Commented Dec 23, 2011 at 13:55
  • 6
    For anyone running HEAD@{1} in Powershell (Windows), you'll get a non descriptive error like error: unknown switch 'e'. This is because curly braces have special meaning in Powershell. You'll want to type this instead: 'HEAD@{1}'. Commented Jan 14, 2014 at 0:20
  • 1
    If you are using fish shell, you will get this error merge: HEAD@1 - not something we can merge, fish removes the curly braces so switch to bash first, then it will work.
    – briankip
    Commented Feb 16, 2016 at 9:46
  • 4
    Instead of merging you can also cherry-pick the detached commits, like git cherry-pick HEAD@{3}. This doesn't clutter the history with lots of merges if there are several commits to rescue.
    – DarkDust
    Commented Aug 17, 2016 at 15:12
  • Git for Windows seems to need quotes for the HEAD@{1}, otherwise it comes with strange errors....error: unknown switch c'` -> so use git merge "HEAD@{1}" Commented Feb 13, 2019 at 19:22
94

If your detached HEAD is a fast forward of master and you just want the commits upstream, you can

git push origin HEAD:master

to push directly, or

git checkout master && git merge [ref of HEAD]

will merge it back into your local master.

3
  • 1
    This worked for me and @Josh Lee's approach didn't. So I'm glad this worked!
    – vy32
    Commented May 14, 2013 at 0:27
  • 1
    That pushes the commits on the detached head back to origin (origin/master), but leaves you in a detached state locally. Is git checkout origin master the best way to get back on the master branch? It would seem better to merge into master first, then push back to origin.
    – StuWeldon
    Commented Aug 20, 2014 at 21:38
  • works for me thanks.
    – Nouman Ch
    Commented Dec 3, 2018 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.