{"id":312073,"date":"2021-07-30T15:09:54","date_gmt":"2021-07-30T12:09:54","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/"},"modified":"2021-07-30T15:09:54","modified_gmt":"2021-07-30T12:09:54","slug":"how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/","title":{"rendered":"#How to Fix, Edit, or Undo Git Commits (Changing Git History) \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a30dd93e3f4d\" 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-6a30dd93e3f4d\" 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-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#Adding_New_Changes_To_Commits\" >Adding New Changes To Commits<\/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-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#Changing_Just_The_Git_Commit_Message\" >Changing Just The Git Commit Message<\/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-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#Unstaging_Changes_From_Commits\" >Unstaging Changes From Commits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#Need_To_UndoRemove_a_Commit_Use_Reverting\" >Need To Undo\/Remove a Commit? Use Reverting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#Use_Rebasing_For_Anything_More_Complicated\" >Use Rebasing For Anything More Complicated<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-edit-or-undo-git-commits-changing-git-history-cloudsavvy-it\/#What_If_You_Want_To_Go_Back\" >What If You Want To Go Back?<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Fix, Edit, or Undo Git Commits (Changing Git History) \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\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, 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 1200w\" sizes=\"auto, 400w, 1200w\" 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>Git\u2019s commit history is designed to be immutable (for the most part) and track every change in your project so you never lose work. However, sometimes it\u2019s necessary to rewrite Git history, so Git provides a few tools for editing existing commits.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Adding_New_Changes_To_Commits\"><\/span>Adding New Changes To Commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The most common use case for this is when you make a commit message, and then, before pushing to your remote, realize that you messed up and need to make a small change. You could of course just make a second commit, but that\u2019s unnecessary, and also shows all your coworkers your dumb mistake when you eventually push to the remote.<\/p>\n<p>If you\u2019re simply adding changes, you can use <code>git commit --amend<\/code>. This modifies the most recent commit, and merges in the additional changes that you\u2019ve staged.<\/p>\n<p>First, you\u2019ll need to stage your changes:<\/p>\n<pre>git add .<\/pre>\n<p>And then amend:<\/p>\n<pre>git commit --amend\u00a0--no-edit<\/pre>\n<p>The <code>--no-edit<\/code>\u00a0flag will make the command not modify the commit message. If you need to clarify the new changes in a new message, leave this flag out, and you\u2019ll be prompted for the new commit message.<\/p>\n<p>Under the hood, the amend command makes a new commit with the extra changes and then completely replaces the source commit in the Git history. The old commit is still accessible from <code>git reflog<\/code>\u00a0(more on that below), but going forward, the new commit is the only one that exists. When you push to a remote repo, there\u2019s no way to know that the commit was amended, it\u2019s a purely local change.<\/p>\n<p>Because of this, you will not want to amend commits that have already been pushed, as you will run into many problems and need to forcibly push to the remote, which is not good for anybody.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Changing_Just_The_Git_Commit_Message\"><\/span>Changing Just The Git Commit Message<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you don\u2019t need to make any changes, and just want to fix a typo, you can run <code>amend<\/code>\u00a0without any changes as well:<\/p>\n<pre>git commit --amend -m \"an updated commit message\"<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Unstaging_Changes_From_Commits\"><\/span>Unstaging Changes From Commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Git\u2019s amend command only works if you\u2019re strictly\u00a0<em>adding<\/em> changes. When we say \u201cadded,\u201d we don\u2019t just mean new lines of code; changing a line of code is also adding changes. Removing a line of code, or an entire file, is also a change being added, even though it\u2019s removing data from the project when that change is <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>lied.<\/p>\n<p>However, there are also cases where you might want to actually remove changes from commits. For example, say you ran:<\/p>\n<pre>git add .&#13;\ngit commit<\/pre>\n<p>And added every change in your repo to the staged changes, and committed it, before realizing, \u201coh crap! I didn\u2019t mean to commit that one file!\u201d In this case, you would need to send all the changes back to staging, and then manually unstage the files you don\u2019t want to push.<\/p>\n<p>The solution is to perform a reset, removing the commit and sending the changes back. There are a few kinds of resets, but they all involve taking commits from Git\u2019s history and sending them back to either staging, the local directory, or straight to the trash.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12969\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/f5026f58.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"408\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this case, a soft reset is what you want, which will send all changes back to staging. You can use the following shorthand to reset to the commit behind the HEAD, otherwise you will need to grab the reference from <code>git reflog<\/code>:<\/p>\n<pre>git reset --soft HEAD~<\/pre>\n<p>Then, you will need to remove the file you don\u2019t want committed. The way to do this is actually also a reset, in this case, a mixed reset on a specific file:<\/p>\n<pre>git reset --mixed filename<\/pre>\n<p>This works because resetting this one file will remove the changes from staging, and it won\u2019t get committed when you redo the commit.<\/p>\n<p>You can also do a mixed reset on the whole repo, and <code>git add<\/code>\u00a0all the files except the one you don\u2019t want. This is easier to do if you\u2019re using a GUI Git client.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Need_To_UndoRemove_a_Commit_Use_Reverting\"><\/span>Need To Undo\/Remove a Commit? Use Reverting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Reverting a commit is the easiest way of removing changes. Basically, it takes all the changes from the target commit, and applies the opposite of them. If you created a file, it\u2019s removed. If you removed a line of code, that code is added back. It\u2019s the Git-approved way to \u201cremove\u201d or \u201cundo\u201d a commit, as the original is still kept in the git history.<\/p>\n<p>To use it, run <code>git log<\/code>\u00a0to view the commits:<\/p>\n<pre>git log<\/pre>\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 the reference ID, and then revert the commit:<\/p>\n<pre>git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f<\/pre>\n<p>If you just got stuck in <code>vim<\/code>, press Q, and maybe run <code>git config --global core.editor \"nano\"<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_Rebasing_For_Anything_More_Complicated\"><\/span>Use Rebasing For Anything More Complicated<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Rebasing is essentially a method of moving commits around in your repository. Rather than merging, rebase rewrites git history to move individual commits to a new location. The original commits are left dangling and are removed from the official Git history, though they\u2019re still there in <code>git reflog<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-866\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/86db9305.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"200\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We won\u2019t get into the exact details of it here, but if you\u2019re interested in how it works, you can read our guide to using <code>git rebase<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_If_You_Want_To_Go_Back\"><\/span>What If You Want To Go Back?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Luckily, Git keeps a record of every single change, even when you break the rules and rewrite history.<\/p>\n<p>Every time your branch tip is updated for any reason, Git stores the state of the directory contents before the update in the\u00a0<em>Reference Log,<\/em> or <code>reflog<\/code>. You can view the log with <code>git reflog<\/code>:<\/p>\n<pre>git reflog<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13068\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/ecf50827.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"190\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Many of these will be actual commits, but it also includes other changes. If you need to go back to any individual change, you can perform a hard reset:<\/p>\n<pre>git reset --hard <!--StartFragment -->fdb9db9<\/pre>\n<p>This provides a nice safety net, but you should be warned that reflog only tracks changes that were actually committed, not just staged, and it only tracks movements of the branch tip. Additionally, reflog only keeps entries for 90 days. After that, you\u2019ll only be able to reset back to actual commits.<\/p>\n<\/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\/13067\/how-to-fix-edit-or-undo-git-commits-changing-git-history\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Fix, Edit, or Undo Git Commits (Changing Git History) \u2013 CloudSavvy IT&#8221; Git\u2019s commit history is designed to be immutable (for the most part) and track every change in your project so you never lose work. However, sometimes it\u2019s necessary to rewrite Git history, so Git provides a few tools for editing existing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":312074,"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-312073","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\/312073","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=312073"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/312073\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/312074"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=312073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=312073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=312073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}