<< Date and time in Java 8 | Home | Fun demo of Watson IoT >>

Git reflog to the rescue

Earlier today while slaving away on my code I did some wizard-like changes and solved an issue I had been having with packaging Visual Studio Tools for Office (VSTO) add-ins for Microsoft Outlook. It was soooo cool that I just had to record a video on it as it involved a number of steps and it would be easier to record a video than describing it in text. So I commited my changes and reverted to an easier commit without thinking. And boom!! All my changes was gone - including some stuff I actually needed. I shouldn't have reset to a commit but instead I should have created a new branch from an earlier commit. Stupid is as stupid does!

After making a loud noise I remembered that all was still safe as I'm using Git...

All changes in Git are recorded in the reflog so my work wasn't lost - I just needed to revert back to them which is easy using the reflog. Now I will not go into details on the reflog here but do look it up. All I needed was two simple commands:

  • git reflog (display the reflog and note the commit hash I wanted to go back to)
  • git reset --hard <commit hash>
Below is a more detailed example. The git reflog command shows the log of what has happened. The top entry is me screwing things up. The commit just under it (41a2195) is the working copy as it was and where I wanted to return before branching correctly. Doing the reset command specifying the hash returns me to it.

$ git reflog
afc548e HEAD@{0}: reset: moving to afc548e1029839175a7620b5c15c6a122a4db47c
41a2195 HEAD@{1}: commit (amend): Update to v. 1.0.3.0 across the board to allow update
71ab438 HEAD@{2}: commit: Update to v. 1.0.3.0 across the board to allow update
afc548e HEAD@{3}: rebase -i (finish): returning to refs/heads/master
$ git reset --hard 41a2195
$ git reflog
41a2195 HEAD@{0}: reset: moving to 41a2195
afc548e HEAD@{1}: reset: moving to afc548e1029839175a7620b5c15c6a122a4db47c
41a2195 HEAD@{2}: commit (amend): Update to v. 1.0.3.0 across the board to allow update
71ab438 HEAD@{3}: commit: Update to v. 1.0.3.0 across the board to allow update
afc548e HEAD@{4}: rebase -i (finish): returning to refs/heads/master
Again - as so many times before - thank you Linus :) And now - please go read up on the git reflog if this is news to you...

Tags : ,


Avatar: Andrew Magerman

Re: Git reflog to the rescue

 Glad to see you're liking Git!


Add a comment Send a TrackBack