{"id":3522,"date":"2011-04-11T00:51:01","date_gmt":"2011-04-10T19:21:01","guid":{"rendered":"https:\/\/2thenew.xyz\/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:\/\/2thenew.xyz\/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":6,"footnotes":""},"categories":[7],"tags":[4306,4307],"class_list":["post-3522","post","type-post","status-publish","format-standard","hentry","category-grails","tag-commits-in-git","tag-recovering-lost-commits-in-git"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Steps for recovering lost Commits in GIT\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Hitesh Bhatia\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"How to Recover lost Commits in GIT | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Steps for recovering lost Commits in GIT\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to Recover lost Commits in GIT | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Steps for recovering lost Commits in GIT\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#article\",\"name\":\"How to Recover lost Commits in GIT | TO THE NEW Blog\",\"headline\":\"How to Recover lost Commits in GIT\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/hitesh\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2011-04-11T00:51:01+05:30\",\"dateModified\":\"2016-12-19T15:29:49+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#webpage\"},\"articleSection\":\"Grails, Commits in GIT, Recovering lost Commits in GIT\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#listItem\",\"name\":\"How to Recover lost Commits in GIT\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#listItem\",\"position\":3,\"name\":\"How to Recover lost Commits in GIT\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/hitesh\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/hitesh\\\/\",\"name\":\"Hitesh Bhatia\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/480688fa251da2d21002803e0b349c1fe911842715b972676a1aa3cb12808e58?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Hitesh Bhatia\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/\",\"name\":\"How to Recover lost Commits in GIT | TO THE NEW Blog\",\"description\":\"Steps for recovering lost Commits in GIT\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/how-to-recover-lost-commits-in-git\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/hitesh\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/hitesh\\\/#author\"},\"datePublished\":\"2011-04-11T00:51:01+05:30\",\"dateModified\":\"2016-12-19T15:29:49+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to Recover lost Commits in GIT | TO THE NEW Blog","description":"Steps for recovering lost Commits in GIT","canonical_url":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#article","name":"How to Recover lost Commits in GIT | TO THE NEW Blog","headline":"How to Recover lost Commits in GIT","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/hitesh\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2011-04-11T00:51:01+05:30","dateModified":"2016-12-19T15:29:49+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#webpage"},"articleSection":"Grails, Commits in GIT, Recovering lost Commits in GIT"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.xyz\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#listItem","name":"How to Recover lost Commits in GIT"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#listItem","position":3,"name":"How to Recover lost Commits in GIT","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/hitesh\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/hitesh\/","name":"Hitesh Bhatia","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/480688fa251da2d21002803e0b349c1fe911842715b972676a1aa3cb12808e58?s=96&d=mm&r=g","width":96,"height":96,"caption":"Hitesh Bhatia"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/","name":"How to Recover lost Commits in GIT | TO THE NEW Blog","description":"Steps for recovering lost Commits in GIT","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/hitesh\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/hitesh\/#author"},"datePublished":"2011-04-11T00:51:01+05:30","dateModified":"2016-12-19T15:29:49+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"How to Recover lost Commits in GIT | TO THE NEW Blog","og:description":"Steps for recovering lost Commits in GIT","og:url":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"How to Recover lost Commits in GIT | TO THE NEW Blog","twitter:description":"Steps for recovering lost Commits in GIT","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"3522","title":null,"description":"Steps for recovering lost Commits in GIT","keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:15:00","updated":"2024-02-29 10:59:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to Recover lost Commits in GIT\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Grails","link":"https:\/\/2thenew.xyz\/blog\/category\/grails\/"},{"label":"How to Recover lost Commits in GIT","link":"https:\/\/2thenew.xyz\/blog\/how-to-recover-lost-commits-in-git\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/3522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=3522"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/3522\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=3522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=3522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=3522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}