252

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Travis
  • 3,156
  • 3
  • 21
  • 27

2 Answers2

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

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 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 http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html very useful – rgardler Dec 23 '11 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}'. – Johnny Oshika Jan 14 '14 at 00: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 Feb 16 '16 at 09: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 Aug 17 '16 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}"` – Falco Alexander Feb 13 '19 at 19:22
  • To get back your work: 1. Run `git reflog` 2. Find the SHA of the commit e.g. `abcd123 ` 3. Run `git merge abcd123 ` – jon Feb 13 '19 at 22:16
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.

richo
  • 8,717
  • 3
  • 29
  • 47
  • 1
    This worked for me and @Josh Lee's approach didn't. So I'm glad this worked! – vy32 May 14 '13 at 00: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 Aug 20 '14 at 21:38
  • works for me thanks. – Nouman Ch Dec 03 '18 at 05:31