Tuesday, March 8, 2011

Oh Shit! Git Amend Ate My Changes!

Today a developer did a git commit --amend and accidentally overwrote his entire HEAD. (I'm not sure how, but it did happen.) I knew that since git saves (what seems like) everything, if we could find the commit hash for his previous HEAD that we'd be able to cherry-pick it. We scrolled up through his terminal but couldn't find any reference to it =/ So that meant we needed to try finding it the hard way: .git/

So then I started looking through the git-dir and after a little digging, we found therein a log file for each branch (.git/logs/refs/heads/*), the contents of which lists the last several commit hashes made to the branch respectively. Alright! Each file contains a somewhat chronological list of the hashes generated by each change to the tree (rebase, pull, commit, cherry-pick, etc.). We were able to use git show on the hashes near the end of our branch's log file and recover the change!

Conclusion, steps to recover:
  1. git branch -- # e.g. my-feature
  2. tail .git/logs/refs/heads/my-feature
  3. git show $hash # using the hashes from the log file until you find the one you want
UPDATE:
So it turns out the easiest way to do this is to use git reflog, which will list all the changes to your HEAD for the past X changes. Way easier!

No comments:

Post a Comment