How to Recover a Lost Commit in GIT

The first thing we need to do when we "lost" a git commit is basically relax. Because everything in Git is tracked. If your commit exists then your commit exists forever (well, almost). The only thing to do is to find them.




Commit Early Commit Often

The problem when doing long hours of code is forgetting that everything is not eternal. Blackout. Broken laptop. Damaged hard drive. Tsunami. You may remember all the lines of your work, but spending hours to write it down again is exhausted.

We need to commit it and make sure everything is tracked. It's like punching Cmd + S multiple times like you have OCD.

Git Reflog is Our Friend

From the git-scm reference: 

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

This is our Limbo. When we make a commit, and it's gone we know a place to pick it up. Bear in mind that everything inside this Limbo can be purged and cleaned after 30 to 90 days, depends on our git configuration. And this log is from our local repository. So if you just clone a repository from somewhere, you get nothing.

Saving Private Commit

I accidentally "undo" my last 2 commits. Now I can't see them. This issue also often happens when you use the unstable version of a git (GUI) client.

This sample repo does not represent the actual issue of mine, since I can't reproduce it. 

First, we may see something like this after our commits disappeared :

laylamajnun:repo $ git logk 

* 009f646 (HEAD -> master) Modify the first file

* 85b45ad Add the second file

* dc37721 Add first file

In fact, I already add 03.txt and 04.txt to the repository. 

Our mission begins.

laylamajnun:repo $ git reflog --all

009f646 (HEAD -> master) refs/heads/master@{0}: reset: moving to HEAD~1

009f646 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~1

7d4e624 refs/heads/master@{1}: reset: moving to HEAD~1

7d4e624 HEAD@{1}: reset: moving to HEAD~1

9b64dca refs/heads/master@{2}: commit: Add more file

9b64dca HEAD@{2}: commit: Add more file

7d4e624 refs/heads/master@{3}: commit: Add the third file

7d4e624 HEAD@{3}: commit: Add the third file

009f646 (HEAD -> master) refs/heads/master@{4}: commit: Modify the first file

009f646 (HEAD -> master) HEAD@{4}: commit: Modify the first file

85b45ad refs/heads/master@{5}: commit: Add the second file

85b45ad HEAD@{5}: commit: Add the second file

dc37721 refs/heads/master@{6}: commit (initial): Add first file

dc37721 HEAD@{6}: commit (initial): Add first file

As we can see, the third and fourth commit is there. The commit IDs are 7d4e624 and 9b64dca respectively. 

laylamajnun:repo $ git merge 9b64dca

Updating 009f646..9b64dca

Fast-forward

 03.txt | 0

 04.txt | 0

 2 files changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 03.txt

 create mode 100644 04.txt

laylamajnun:repo $ git logk

* 9b64dca (HEAD -> master) Add more file

* 7d4e624 Add the third file

* 009f646 Modify the first file

* 85b45ad Add the second file

* dc37721 Add first file

If you need more time to check your work, you can use git checkout [commit ID] before doing the next things. Again, this repo is only a sample. Things can be worse, but I hope you can get a grip.

Comments

Popular Posts