Brool brool (n.) : a low roar; a deep murmur or humming

Recovering From A --hard Reset In Git

 |  reset hard git coding recovering

I was switching between git repositories the other day, and managed to do a “git reset –hard HEAD\^” in the wrong repository. Which wasn’t bad, since I had most of the files already open in Emacs… but then Emacs calmly told me that it was re-reading the files from disk. But, git had everything still around – it turns out to be pretty easy to get it back. The magic command turned out to be git reflog.

$ git reflog aba2b93... HEAD@{0}: reset --hard HEAD^ 28a0c01... HEAD@{1}: commit: more work on pre-receive 1c4a3af... HEAD@{2}: merge tmp: Fast forward 84d69cb... HEAD@{3}: checkout: moving to commit_hooks 1c4a3af... HEAD@{4}: commit: commit hooks a489ebd... HEAD@{5}: checkout: moving to tmp a489ebd... HEAD@{6}: checkout: moving to a489ebd

So I had lost everything in HEAD@{1}, but you can get it back by just checking out that particular commit.

$ git checkout HEAD@{1} Note: moving to "28a0c01" which isn't a local branch If you want to create a new branch from this checkout, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b HEAD is now at 28a0c01... commit: more work on pre-receive $ git checkout -b tmp ... do whatever you need to do to get tmp to the right state ... $ git checkout master $ git merge tmp

Note that if I didn’t need to fiddle around with stuff, but just had wanted everything in the commit, I could just have merged the entire commit:

$ git merge HEAD@{1}

… and that would have brought everything back to the state I needed.

Discussion

Comments are moderated whenever I remember that I have a blog.

There are no comments on this article.

Add a comment