{"id":309180,"date":"2021-07-27T14:00:17","date_gmt":"2021-07-27T11:00:17","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/"},"modified":"2021-07-27T14:00:17","modified_gmt":"2021-07-27T11:00:17","slug":"how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/","title":{"rendered":"#How Does Git Reset Actually Work? Soft, Hard, and Mixed Resets Explained \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-6a28c86472a94\" 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-6a28c86472a94\" 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-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/#What_Is_The_Git_HEAD\" >What Is The Git HEAD?<\/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-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/#How_Does_Git_Reset_Work\" >How Does Git Reset Work?<\/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-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/#Using_Git_Reset\" >Using Git Reset<\/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-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained-cloudsavvy-it\/#Should_You_Ever_Hard_Reset\" >Should You Ever Hard Reset?<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How Does Git Reset Actually Work? Soft, Hard, and Mixed Resets Explained \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><code>git reset<\/code>\u00a0is a powerful command that can modify the history of your Git repository and fix mistakes you made. However, while the name makes it seem scary to use, it\u2019s actually pretty easy, so lets talk about how it works.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_The_Git_HEAD\"><\/span>What Is The Git HEAD?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before understanding how resets work, we need to talk about the Git HEAD.<\/p>\n<p>\u201cHEAD\u201d is simply an alias for your current working commit. You can think of it like a record player head, where the position of it determines what data is being used. If you\u2019re currently using the <code>master<\/code>\u00a0branch, the HEAD will be at the latest commit in that branch.<\/p>\n<p>Despite being an alias for a commit, the HEAD doesn\u2019t actually point directly to a commit most of the time. It points towards a branch, and automatically uses the latest commit. It can also point towards a commit directly, which is known as \u201cdetached HEAD,\u201d though that doesn\u2019t matter for <code>git reset<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_Does_Git_Reset_Work\"><\/span>How Does Git Reset Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Git\u2019s commit history is stored as a tree of commits, and is intended to be immutable for the most part. However, sometimes it\u2019s necessary to modify this history, and that\u2019s where <code>git reset<\/code>\u00a0comes into play.<\/p>\n<p>Each commit links to the commit before it, and can branch off into different limbs that will eventually get merged back into the <code>master<\/code>\u00a0branch. In either case, the HEAD will usually point towards the latest commit in whatever branch you\u2019re working on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12967\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/5ad965dd.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"273\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>So what h<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>ens when you make a commit you want to revert?<\/p>\n<p>Well, running <code>git reset<\/code>\u00a0basically moves the HEAD back, and leaves all the commits in front of it hanging. This rewrites the usually immutable Git history to get rid of the commits in front of the HEAD.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12968\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/c25cf2df.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"292\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This can be quite useful for a lot of reasons. Perhaps you made a commit, then made some additional changes, and wanted to push the whole thing as one commit. You could <code>git reset<\/code>\u00a0back to the previous commit, and then re-commit correctly.<\/p>\n<p>Or perhaps you accidentally made a commit that included some changes you didn\u2019t want to track. This can be very hard to figure out if you don\u2019t know how <code>git reset<\/code>\u00a0works, but resetting the HEAD and then only staging the correct changes will achieve this. Note that this is different from <code>git revert<\/code>, which reverses commits.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How To Recover Reverted Commits In a Git Repository<\/em><\/strong><\/p>\n<p>There are three different kinds of resets in Git, and they all differ based on how they handle the commits that get left hanging. They all rewrite Git history, and they all move the HEAD back, but they deal with the changes differently:<\/p>\n<ul>\n<li><code>git reset --soft<\/code>, which will keep your files, and stage all changes back automatically.<\/li>\n<li><code>git reset --hard<\/code>, which will <em>completely destroy any changes and remove them from the local directory<\/em>. Only use this if you know what you\u2019re doing.<\/li>\n<li><code>git reset --mixed<\/code>, which is the default, and keeps all files the same but unstages the changes. This is the most flexible option, but despite the name, it doesn\u2019t modify files.<\/li>\n<\/ul>\n<p>The difference between soft and mixed is whether or not the changes are staged. Staged is basically an in-between zone between the local directory and Git history. <code>git add<\/code>\u00a0stages files, and <code>git commit<\/code>\u00a0writes them to the history. In either case, the local directory is unaffected, it just changes the state of Git\u2019s tracking of those changes.<\/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>Basically, Soft and Mixed resets are mostly the same and allow you to keep the changes, and Hard resets will completely set your local directory back to where it was at the time of the commit.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Git_Reset\"><\/span>Using Git Reset<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you understand what\u2019s going on, actually using <code>git reset<\/code>\u00a0is incredibly easy. To reset, you\u2019ll need a reference to the commit you want to move back to. You can get this by running <code>reflog<\/code>:<\/p>\n<pre>git reflog<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12973\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/ee27a6ad.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"678\" height=\"246\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Copy the seven digit code on the right. If you just got stuck in <code>vim<\/code>, press Q, and maybe run <code>git config --global core.editor \"nano\"<\/code>.<\/p>\n<p>Then, you can reset back to the target commit:<\/p>\n<pre>git reset\u00a0--mixed <!--StartFragment -->a560612<\/pre>\n<p>Or, you can target commits based on their position relative to the HEAD. The following command targets the commit right before the HEAD, which is useful shorthand if you need to reset the latest commit:<\/p>\n<pre>git reset --mixed\u00a0HEAD~<\/pre>\n<p>If you\u2019re using a Git client like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-fork.com\/\">Fork<\/a>, resetting is as easy as right clicking the target commit and selecting \u201cReset\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12975\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/c52424a4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"471\" height=\"228\" 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=\"Should_You_Ever_Hard_Reset\"><\/span>Should You Ever Hard Reset?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You should really only be using soft or mixed resets, but, if you messed up your repository so bad that you need to completely reset it, you can use the following sequence of commands to full set it back to normal.<\/p>\n<pre>git fetch origin&#13;\ngit checkout master&#13;\ngit reset --hard origin\/master&#13;\ngit clean -d --force<\/pre>\n<p>Of course, you could always pull the classic \u201cdelete your repository and re-clone from Github,\u201d but this way is at least Git approved, and only resets a single branch.<\/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\/12962\/how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How Does Git Reset Actually Work? Soft, Hard, and Mixed Resets Explained \u2013 CloudSavvy IT&#8221; git reset\u00a0is a powerful command that can modify the history of your Git repository and fix mistakes you made. However, while the name makes it seem scary to use, it\u2019s actually pretty easy, so lets talk about how it works&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":309181,"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-309180","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\/309180","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=309180"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/309180\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/309181"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=309180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=309180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=309180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}