{"id":6541,"date":"2013-11-01T14:10:12","date_gmt":"2013-11-01T21:10:12","guid":{"rendered":"http:\/\/www.analogrithems.com\/rant\/?p=6541"},"modified":"2013-11-01T14:10:12","modified_gmt":"2013-11-01T21:10:12","slug":"recover-lost-git-commits","status":"publish","type":"post","link":"https:\/\/www.analogrithems.com\/rant\/recover-lost-git-commits\/","title":{"rendered":"How to recover lost git commits"},"content":{"rendered":"<p>We&#8217;ve all been there, your working on a large group of code with some developers and you write a chunk of code for a new feature and commit it. \u00c2\u00a0A few weeks later you go to use it and find somehow somewhere someone deleted your code. \u00c2\u00a0It&#8217;s gone never to be seen again. \u00c2\u00a0But hey, this is git, it promised to keep track of all of our code for us. \u00c2\u00a0It&#8217;s got to be in there somewhere still right? \u00c2\u00a0Yes and along comes git grep to the rescue. \u00c2\u00a0If you can recall the name of some variables you may have used in your code you can use git grep to search the commit history to find it.<\/p>\n<p>One thing you need to keep in mind if you have a repo with more then one branch you may need to switch branches to search the different commit histories<\/p>\n<h3>Pay dirt!<\/h3>\n<pre>[analog@dev htdocs]# git grep 'display_pri' $(git rev-list --all)\r\n6e6242576c9e4874b0edbb0cceb9adec1b380a47:.upgradeScripts\/displayPrice.php:                                              $json['prices'][$i]['display_price'] = 1;\r\n6e6242576c9e4874b0edbb0cceb9adec1b380a47:.upgradeScripts\/displayPrice.php:                                              $json['prices'][$i]['display_price'] = 0;<\/pre>\n<p> So what are we doing here?  I&#8217;m asking git to search all the commits listed with <b>git rev-list &#8211;all<\/b> for a string that contains <b>display_pri<\/b>  Right away you see it finds a commit that has some code with that string.  Now we can use <b>git checkout 6e6242576c9e4874b0edbb0cceb9adec1b380a47<\/b>  to switch to that commit like we would a branch.  Do not commit anything in this branch\/commit.  You are now in a detached HEAD, this means that if you commit from here you could overwrite other code and be in the same boat all over again.  Instead checkout the commit in a different place and copy the code to a new current branch to add it back<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve all been there, your working on a large group of code with some developers and you write a chunk of code for a new feature and commit it. \u00c2\u00a0A few weeks later you go to use it and find somehow somewhere someone deleted your code. \u00c2\u00a0It&#8217;s gone never to be seen again. \u00c2\u00a0But hey, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164],"tags":[165],"class_list":["post-6541","post","type-post","status-publish","format-standard","hentry","category-development","tag-git"],"_links":{"self":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/6541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/comments?post=6541"}],"version-history":[{"count":1,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/6541\/revisions"}],"predecessor-version":[{"id":6542,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/posts\/6541\/revisions\/6542"}],"wp:attachment":[{"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/media?parent=6541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/categories?post=6541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.analogrithems.com\/rant\/wp-json\/wp\/v2\/tags?post=6541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}