{"id":3522,"date":"2011-04-11T00:51:01","date_gmt":"2011-04-10T19:21:01","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3522"},"modified":"2016-12-19T15:29:49","modified_gmt":"2016-12-19T09:59:49","slug":"how-to-recover-lost-commits-in-git","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-recover-lost-commits-in-git\/","title":{"rendered":"How to Recover lost Commits in GIT"},"content":{"rendered":"<p>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&#8217;s is the example of how I got my lost commits back.<\/p>\n<p>Below are my 5 recent Commits<\/p>\n<p>[shell]git log  -5 &#8211;pretty=oneline<br \/>\nea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class<br \/>\n25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller<br \/>\n03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File<br \/>\nd3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore<br \/>\n667227783e283c517f4bc879c129f50889f8709c another changes<br \/>\n[\/shell]<\/p>\n<p>I wanted to undo my changes to User controller and I did a hard reset to my commits.<\/p>\n<p>[shell]git reset &#8211;hard 03aa5f44ee9293a2d197edd060720682d6daddb5<br \/>\nHEAD is now at 03aa5f4 modified readMe File[\/shell]<\/p>\n<p>And Head is now set to commit &#8220;modified readMe File&#8221;. 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.<\/p>\n<p>[shell]git log  -5 &#8211;pretty=oneline<br \/>\n03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File<br \/>\nd3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore<br \/>\n667227783e283c517f4bc879c129f50889f8709c another changes<br \/>\n80eb4396fb70c8f59f624fc3e5a2601cd6b986a3 more changes to Demo File<br \/>\n16b6202e925093b08e10a68037e7c64b450f0173 dummy commit[\/shell]<\/p>\n<p>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\u00a0reflog command.<\/p>\n<p>[shell]git reflog<br \/>\n03aa5f4 HEAD@{0}: 03aa5f44ee9293a2d197edd060720682d6daddb5: updating HEAD<br \/>\nea007ac HEAD@{1}: commit: Refactored Person Class<br \/>\n25e48ae HEAD@{2}: commit: modified User controller<br \/>\n03aa5f4 HEAD@{3}: commit: modified readMe File<br \/>\nd3d424d HEAD@{4}: commit: added ipr file to gitignore<br \/>\n6672277 HEAD@{5}: commit: another changes<br \/>\n80eb439 HEAD@{6}: commit: more changes to Demo File<br \/>\n16b6202 HEAD@{7}: commit: dummy commit<br \/>\n[\/shell]<\/p>\n<p>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 &#8220;lostCommit&#8221; with hash &#8220;ea007ac&#8221;.<\/p>\n<p>[shell]git branch lostCommit ea007ac[\/shell]<\/p>\n<p>And Now when I checkout my lostCommit branch and check log. Here is what it shows.<\/p>\n<p>[shell]<br \/>\ngit log  -5 &#8211;pretty=oneline<br \/>\nea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class<br \/>\n25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller<br \/>\n03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File<br \/>\nd3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore<br \/>\n667227783e283c517f4bc879c129f50889f8709c another changes [\/shell]<\/p>\n<p>I got my lost commits. And all other changes that I thought were lost.<\/p>\n<p>To explore more visit. <a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-grep.html\">git grep manual page<\/a> or the git community book which is maintained by Scott Chacon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s is the example of how I got my lost commits back. Below are my 5 recent [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[4306,4307],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3522"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3522"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3522\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}