{"id":371199,"date":"2021-11-23T15:30:12","date_gmt":"2021-11-23T12:30:12","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-remove-a-commit-from-github-cloudsavvy-it\/"},"modified":"2021-11-23T15:30:12","modified_gmt":"2021-11-23T12:30:12","slug":"how-to-remove-a-commit-from-github-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-remove-a-commit-from-github-cloudsavvy-it\/","title":{"rendered":"#How to Remove a Commit From Github \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 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-6aef59242aea5\" 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-6aef59242aea5\" 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-remove-a-commit-from-github-cloudsavvy-it\/#Dont_Do_This_If_You_Can_Avoid_It\" >Don\u2019t Do This If You Can Avoid It<\/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-remove-a-commit-from-github-cloudsavvy-it\/#Fixing_Commits_Locally\" >Fixing Commits Locally<\/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-remove-a-commit-from-github-cloudsavvy-it\/#Reverting_Github_Safe\" >Reverting (Github Safe)<\/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-remove-a-commit-from-github-cloudsavvy-it\/#Force_Reset_Unsafe\" >Force Reset (Unsafe)<\/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-remove-a-commit-from-github-cloudsavvy-it\/#Moving_a_Commit_to_a_Different_Branch\" >Moving a Commit to a Different Branch<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Remove a Commit From Github \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-7125\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.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\/2020\/10\/d6528811.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you accidentally committed something you shouldn\u2019t have, and pushed it to Github, there are still ways to delete or modify it. Usually, you don\u2019t want to mess with Git\u2019s history, but in some cases it can be safe when done properly.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Dont_Do_This_If_You_Can_Avoid_It\"><\/span>Don\u2019t Do This If You Can Avoid It<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Removing commits from Git\u2019s history is <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly a bad idea. Git is meant to track every version of your files, and there\u2019s always alternatives to deletion, like git revert, that will keep the history intact.<\/p>\n<p>Once it\u2019s on a remote repository, like Github, it\u2019s much harder to get rid of the commit entirely. This is because anyone else using the repo will have their own local copy of that repo, and forcibly deleting a commit will make them go out of sync.<\/p>\n<p>There are still a few reasons to do this though\u2014maybe you accidentally committed private data to a public repo, which would still be visible in the history no matter what you do. It\u2019ll cause some issues for your coworkers, but even in this case it\u2019s possible with a force push.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Fixing_Commits_Locally\"><\/span>Fixing Commits Locally<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you\u2019re working locally, and you haven\u2019t pushed your changes to Github yet, you can safely reset or modify commits that aren\u2019t on the remote. Git tracks your local directory, stages changes internally, and keeps a Git history. This is all kept in sync with the remote, but only when running <code>push\/pull<\/code>. Github doesn\u2019t care about the state of your repo until you push.<\/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>If you just made a mistake when committing files, you can do a soft reset and make a new commit. This is actually commonplace and useful if you want to merge multiple commits into one, called \u201csquashing.\u201d If you wanted to completely discard the changes, you can do that as well (with a <code>--hard<\/code>\u00a0reset).<\/p>\n<p>If you haven\u2019t pushed to Github yet, you can revert the most recent commit with a soft reset, which doesn\u2019t effect your local files at all, but makes it so that you never committed the changes to Git in the first place.<\/p>\n<pre>git reset --soft HEAD~<\/pre>\n<p>If you just want to add a new file\/change to the most recent commit, Git has a flag built in to do exactly that:<\/p>\n<pre>git commit --amend\u00a0--no-edit<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Reverting_Github_Safe\"><\/span>Reverting (Github Safe)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you want to undo the changes from a commit you already pushed to Github, the safe way to do that is with a revert. Reverting will generate an \u201copposite commit,\u201d which will basically undo all the changes from a specific commit. If you added a line, it\u2019s removed, and if you deleted a file, it\u2019s added back.<\/p>\n<p>This is safe because it doesn\u2019t change the history, only adds new history on top of it. You can push the revert commit to Github, and have your coworkers run <code>git pull<\/code>\u00a0to get the updates.<\/p>\n<p>First, run <code>git log<\/code>\u00a0to get a list of commits:<\/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>Then, copy the SHA1 hash and revert the commit:<\/p>\n<pre>git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Force_Reset_Unsafe\"><\/span>Force Reset (Unsafe)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you really want to remove a commit, the method to do that is to remove it locally, and then force push to Github. Since this is\u00a0<strong>very dangerous<\/strong> and can mess up your coworker\u2019s local repositories, if you still want to, you may have to disable force push branch protection in Github\u2019s repository settings:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14781\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/7c89bb5b.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1506\" height=\"1052\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, you can remove the commit locally, which is easiest if it\u2019s the latest commit:<\/p>\n<pre>git reset --soft HEAD~<\/pre>\n<p>You can also do an interactive rebase, which is useful if the commit isn\u2019t the most recent one. If the commit was, for example, 12 commits ago, you can rebase from then, remove the offending commit, and save.<\/p>\n<pre>git rebase -i HEAD~12<\/pre>\n<p>Once your local repo is in working order, you can force push to Github.<\/p>\n<pre>git push origin master --force<\/pre>\n<p><strong>Make sure your repo is up to date!<\/strong> If the remote branch has changes that you don\u2019t, they will be overwritten by this push.<\/p>\n<p>You may want to coordinate this with your team members, as you may have to ping them and tell them to run a fetch and reset:<\/p>\n<pre>git fetch&#13;\n&#13;\ngit reset origin\/master --soft<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Moving_a_Commit_to_a_Different_Branch\"><\/span>Moving a Commit to a Different Branch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you accidentally committed to the wrong branch, and want to remove the commit and move it to the correct branch, there are tools in Git to handle that. You can read our guide to moving commits around to learn more, but the same principles <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\u2014if you need to completely remove a commit, you must force reset. If you\u2019re fine with reverting, you can simply revert and cherry-pick.\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\/14779\/how-to-remove-a-commit-from-github\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Remove a Commit From Github \u2013 CloudSavvy IT&#8221; If you accidentally committed something you shouldn\u2019t have, and pushed it to Github, there are still ways to delete or modify it. Usually, you don\u2019t want to mess with Git\u2019s history, but in some cases it can be safe when done properly. Don\u2019t Do This&#8230;<\/p>\n","protected":false},"author":1,"featured_media":371201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-371199","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\/371199","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=371199"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/371199\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/371201"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=371199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=371199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=371199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}