Git lost commits Git lost commits git git

Git lost commits


To get your HEAD back in the right place:

  1. git reflog to get a list of where HEAD has been lately.
  2. git show sha1 to find the spot you want your HEAD to be.
  3. Once you find the commit you want, git merge to get your master back into the right spot.

Some explanation: In a git commit there is nothing pointing one commit to the one that happend after it. When you reset the HEAD, you pointed it to an older commit. Your previous head is now dangling without anything pointing to it.

We use reflog to see where HEAD has been lately. Once it is set back to where you want it, you point the master, or some other, branch back to that spot and all is well!


I did it a little differently. I did...

git reflog3bd79d2 HEAD@{2}: checkout: moving from edbfb06528c43586a0e0e10a73051e06980b9281 to masteredbfb06 HEAD@{3}: commit: added general comments for rubricf8ca172 HEAD@{4}: checkout: moving from 904d63bf08f6f6b1494bfa473b158b9509b18423 to 904d63b HEAD@{10}: commit: updated results page and csv933f2a6 HEAD@{11}: commit: updatesf56e6cd HEAD@{12}: clone: from git@heroku.com:xxxx.git

...in this case my "added general comments for rubric" was the commit that I lost. Now that I have the commit ID I used cherry-pick to get it back...

git cherry-pick edbfb06


You shouldn't need to push anything from another repo after a git reset --hard.

git reflog should allow you to locate the lost commits (which are actually unreferenced, but still there in your Git repo, by default until 90 days: see git config gc.reflogexpire. You can even make sure it never expires if you really want that).
See for instance Undoing a git reset --hard HEAD~1 or restore - git reset --hard HEAD^ as examples.