6

I wanted to clear my working directory of some uncommitted files, but accidentally ran git reset --hard.

I realized that I had lost the previous (un-pushed) commit, so I ran git reset --hard ORIG_HEAD. This did not get me to my lost commit.

I ran git reflog, but the commit was not listed there. I also ran git fsck --lost-found, but there were no commits in the list, only a few unrelated blobs and trees.

Since I can't find any reference of the lost commit (apart from the .git/COMMIT_EDITMSG which still has the relevant commit message and list of changes), I'm not sure how to go about recovering the commit.

Is there any way to get the lost commit back, or should I get ready for an all-nighter?

Niel Thiart
  • 628
  • 7
  • 13
  • Why do you think you had a git commit for your local changes? git reset wouldn't have lost it. If you had only uncommitted changes, those are gone. – fche Mar 18 '13 at 15:15
  • 4
    `git reset --hard` with no commit-ish argument is equivalent to `git reset --hard HEAD`, which will not lose commits, pushed or unpushed. It will simply reset your index and working directory back to the state of the last commit you made, losing staged and unstaged (but not yet committed) changes. The `git reset --hard ORIG_HEAD` is likely to be a problem, depending on what exactly the last command that actually updated ORIG_HEAD was, and how long ago it was, and what you've done in between... – twalberg Mar 18 '13 at 15:42
  • Thanks, the lost commit was made about half an hour before the `git reset --hard`. – Niel Thiart Mar 18 '13 at 15:56
  • @twalberg How do you get to see those commits after you have reset the index? Is there a way to add them back to the index? – Setafire Jul 17 '15 at 21:00
  • 1
    @Setafire I would recommend checking out `git reflog` for that... – twalberg Jul 17 '15 at 21:38
  • Wow @twalberg, you saved me! Thanks! – oleggromov May 20 '16 at 11:48

1 Answers1

10

Not sure why you are unable to find your commit, as @twalberg's comment about git reset --hard is correct. Here are some things to try, though.

You have the message for the commit you're looking for (.git/COMMIT_EDITMSG). If COMMIT_EDITMSG was written, then that particular commit should be somewhere. Pick out some text from the message that is fairly unique and try this:

git log -g --grep="<something specific from your commit message>"

It will go through the reflog and find commits that match text from your lost commit's message.

If no luck with that, you can try looking through all commits on every branch:

git log --all --grep="<something specific from your commit message>"

Once you find the commit hash, you can check it out, make a new branch, merge it back into your current branch, etc.

However, if that all fails you can try looking though objects that are in the repository, but aren't part of any commit (e.g., added to the index, but not committed.) This answer can help you with that:

https://stackoverflow.com/a/7376959/845716

Community
  • 1
  • 1
Rob Bajorek
  • 6,382
  • 7
  • 44
  • 51
  • 1
    Thanks Rob. I had searched the git logs before I posted the question, but your final link ["Undo git reset --hard"](http://stackoverflow.com/a/7376959/845716) to look through all the objects worked! No idea why I can't find the commit though. I can find the individual files. – Niel Thiart Mar 18 '13 at 17:38