12

Unfortunately I did several times git reset --hard HEAD^ losing a quite big chunk of code in several files. Is there a way to restore those commits or in this case to forward where the HEAD was before, so I can bring up those lines that I lost?

2 Answers 2

17

Use the reflog to recover the sha1 of the previous HEAD. In particular, the article reflog, your safety net will be particularly relevant to you. From that article:

The most common usage of this command is that you’ve just done a git reset and moved your HEAD back a few commits. But oops, you need that bit of code you left in the second commit. Crap. Now what?

Once you have found the sha1 of the commit you want to go back to, use something like:

git reset --hard 0a1b2c
4
  • 3
    You can also reset to HEAD@{n}, finding the appropriate n from the reflog Jul 15, 2010 at 22:57
  • One can also view the reflog with git log -g, which might provide a bit more context than git reflog show. Jul 16, 2010 at 4:04
  • 1
    Cheers, you just saved my ass big time. Apr 3, 2011 at 20:31
  • or reflog with date reference: git reset --hard "HEAD@{1 million years ago}"
    – J-16 SDiZ
    Dec 22, 2011 at 3:21
0

Run git reset --hard HEAD@{1} if you just committed your code and you want to undo that.

See Section called "Ordinal Spec" at http://book.git-scm.com/4_git_treeishes.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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