{"id":296892,"date":"2021-07-12T15:13:22","date_gmt":"2021-07-12T12:13:22","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it\/"},"modified":"2021-07-12T15:13:22","modified_gmt":"2021-07-12T12:13:22","slug":"how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it\/","title":{"rendered":"#How To Recover Reverted Commits In a Git Repository \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26b736c7788\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26b736c7788\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it\/#Dont_Panic\" >Don\u2019t Panic<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it\/#Fixing_Reverted_Commits\" >Fixing Reverted Commits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-recover-reverted-commits-in-a-git-repository-cloudsavvy-it\/#Reverting_Without_a_Trace\" >Reverting Without a Trace<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Recover Reverted Commits In a Git Repository \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-2266\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Git logo\" width=\"700\" height=\"299\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you\u2019ve just reverted a Git commit, and accidentally deleted a file or piece of code because of it, don\u2019t worry. Git keeps track of everything, and recovering the reverted file is easy to do both from the command line and most GUI Git clients.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Dont_Panic\"><\/span>Don\u2019t Panic<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Luckily, Git keeps track of everything, even reverted commits. You haven\u2019t deleted anything yet.<\/p>\n<p>You may have run into this while trying to \u201cdelete\u201d a Git commit. However, you can\u2019t really delete commits once you make them. You can only reset to a previous commit and ignore the local changes, or revert the changes.<\/p>\n<p>For example, in my case with this problem, I had just initialized my Git repository, and accidentally staged everything without writing my <code>.gitignore<\/code>\u00a0file, which committed <code>.\/bin\/<\/code>\u00a0folders and other junk I didn\u2019t want. So, without thinking about it, I clicked \u201crevert\u201d in my Git client, and watched in horror as my entire directory got deleted. Oops.<\/p>\n<p>The problem is that \u201creverting\u201d a commit is not the same as unstaging the changes, and can have unexpected effects on your local files that can only be fixed through Git. This looks like a serious problem until you realize that all the data is still stored in the <code>.git\/<\/code>\u00a0folder of your repository, so it can be recovered.<\/p>\n<p>It doesn\u2019t help that it\u2019s weirdly named, and the actual way to remove an unwanted commit is to do a \u201csoft reset\u201d back to the last commit behind the HEAD. So, next time, if you\u2019d like to undo a commit (to edit your changes and re-commit) use the following command:<\/p>\n<pre>git reset HEAD~1<\/pre>\n<p>For clarification\u2014this is not a \u201chard\u201d reset, which removes all your local changes as well.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Fixing_Reverted_Commits\"><\/span>Fixing Reverted Commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The fix is pretty simple. Whenever you do a \u201cgit revert,\u201d Git makes a new commit with opposite changes to the commit being reverted. If you created a file, that file is removed, and the commit reflects that.<\/p>\n<p>The fix is to <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ly that reverting commit, and then <em>revert it back<\/em>, which will un-revert the changes. This will work even if the revert commit is not at the HEAD of your repository; otherwise, doing a hard reset would also work and would free your repo from embarrassing \u201crevert\u201d and \u201cunrevert\u201d commits that your coworkers will laugh at.<\/p>\n<p>To find the hash for the commit, you can run <code>git log<\/code>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12608\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/5b6c2c8a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"619\" height=\"277\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Copy this hash, and then run <code>git revert<\/code>:<\/p>\n<pre>git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f<\/pre>\n<p>This should fix the repository, though you may have to commit the revert manually if the automatic commit failed.<\/p>\n<p>In most GUI Git clients, the process is extremely simple. Just click \u201crevert\u201d on the commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12618\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/4bb94da8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"626\" height=\"340\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Reverting_Without_a_Trace\"><\/span>Reverting Without a Trace<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you\u2019re at the HEAD of your commit tree, and you\u2019d like to do this without making new commits, and you haven\u2019t pushed the change yet, you can hard reset your local branch to the old commit.<\/p>\n<pre>git reset --hard HEAD^<\/pre>\n<p>The reason this needs to be a hard reset is because a soft reset would still include the unstaged changes from the revert. You want to reset without the local changes.<\/p>\n<p>You could also soft reset and discard the local changes manually.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/12600\/how-to-recover-reverted-commits-in-a-git-repository\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Recover Reverted Commits In a Git Repository \u2013 CloudSavvy IT&#8221; If you\u2019ve just reverted a Git commit, and accidentally deleted a file or piece of code because of it, don\u2019t worry. Git keeps track of everything, and recovering the reverted file is easy to do both from the command line and most GUI&#8230;<\/p>\n","protected":false},"author":1,"featured_media":296893,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-296892","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/296892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=296892"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/296892\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/296893"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=296892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=296892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=296892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}