Learning How to Git: Recover Deleted Commit

Haydar Ali Ismail
Feb 3, 2017 · 3 min read

So, let’s say you mess things up. Now you want to reset your project to a certain commit. Then you accidentally reset your Git project to a certain commit where you just realized that there are some precious commits that you also removed. Unfortunately, you forgot to push those commit and now you were left with pain that will force you to rework everything again :(.

Don’t be so sad, because if you have ever in that kind of situation, there is still a way to recover that.

Remove Our Precious Commit

Let’s check our commit now using the git log --oneline --all --graph --decorate.

So, now you can see that the commit with hash f52ee7b is currently the latest one in our local branch. We make sure that we are not going to push it to the remote repository. So, let’s say we accidentally remove those commit.

Oh no! Now it’s all gone. What I’m going to do? :(. Don’t worry, we still able to recover it.

Check All Git Reference History

Now, all we need to do to restore our deleted commit is to make sure we have the hash code for that particular commit. If you able to take a note of the commit then you don’t have to follow this step. If you don’t, there is still a way to get it.

To check the history of all of our Git commands in the repository, we can use a command called git reflog. It basically stands for reference logs (reflogs) and logging every single reference and action that we have done before. Now let’s try to run git reflog.

Let’s see… Ah! Found it, that’s our commit. One thing to remember is that Git doesn’t remove any commit even though we reset those commits unless it’s already removed by the garbage collector. So, make sure if you encountered this issue, you need to take the reference of the lost commit as soon as possible. Okay, so now our precious f52ee7b commit will be restored!

Restore The Commit

To restore it, we can simply run a hard reset to the commit we want to take. In my case, I will need to run git reset --hard f52ee7b.

Hooray! After doing all of these, you finally can restore your deleted commit.

Wrap Up

This kind of problem is one of the most disastrous cases when you are doing git. The first time I encountered this problem I really do panic (…and of course, I rework everything again). But, since I know this trick I will never worried about losing any of my work ever again. Hopefully, it’s useful for you and thank you for reading.

References

Haydar Ali Ismail

Written by

Still not better at statistics than some engineers and still not better at engineering than some statistician.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade