How to Recover lost Commits in GIT
Recently while working on Git I had accidentally lost my commits. But since Git has a fantastic revision control system, it remembers what I committed. In other words, it records where its HEAD is every time user commits. Here’s is the example of how I got my lost commits back.
Below are my 5 recent Commits
[shell]git log -5 –pretty=oneline
ea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class
25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller
03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
667227783e283c517f4bc879c129f50889f8709c another changes
[/shell]
I wanted to undo my changes to User controller and I did a hard reset to my commits.
[shell]git reset –hard 03aa5f44ee9293a2d197edd060720682d6daddb5
HEAD is now at 03aa5f4 modified readMe File[/shell]
And Head is now set to commit “modified readMe File”. But later it came to my mind that I had also lost refactoring I did to Person class. If Now I check my logs it would be like this.
[shell]git log -5 –pretty=oneline
03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
667227783e283c517f4bc879c129f50889f8709c another changes
80eb4396fb70c8f59f624fc3e5a2601cd6b986a3 more changes to Demo File
16b6202e925093b08e10a68037e7c64b450f0173 dummy commit[/shell]
But since Git is fantastic it always comes to rescue, as I told earlier Git always records where HEAD was every time I commit, which can be seen with Git reflog command.
[shell]git reflog
03aa5f4 HEAD@{0}: 03aa5f44ee9293a2d197edd060720682d6daddb5: updating HEAD
ea007ac HEAD@{1}: commit: Refactored Person Class
25e48ae HEAD@{2}: commit: modified User controller
03aa5f4 HEAD@{3}: commit: modified readMe File
d3d424d HEAD@{4}: commit: added ipr file to gitignore
6672277 HEAD@{5}: commit: another changes
80eb439 HEAD@{6}: commit: more changes to Demo File
16b6202 HEAD@{7}: commit: dummy commit
[/shell]
To get my lost changes all I did was to create a new branch with the lost commit that I wanted back. So here I am creating a branch named “lostCommit” with hash “ea007ac”.
[shell]git branch lostCommit ea007ac[/shell]
And Now when I checkout my lostCommit branch and check log. Here is what it shows.
[shell]
git log -5 –pretty=oneline
ea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class
25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller
03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
667227783e283c517f4bc879c129f50889f8709c another changes [/shell]
I got my lost commits. And all other changes that I thought were lost.
To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon.
Thank you Hitesh, I thought I’m a relative novice with Git and thought I’d lost a lot of work, but your notes helped me out a great deal.
Thanks for sharing!