{"id":1282,"date":"2022-06-22T11:14:19","date_gmt":"2022-06-22T11:14:19","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1282"},"modified":"2022-06-24T04:31:00","modified_gmt":"2022-06-24T04:31:00","slug":"git-reset-vs-git-revert","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/git-reset-vs-git-revert\/","title":{"rendered":"Git Reset Vs Git Revert"},"content":{"rendered":"\n<p>In this article I will explain the usage of two important git commands <code>git reset<\/code> and <code>git revert<\/code>. Both the commands are used to undo changes or commits, but there are significant differences in the way they work. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Git Reset <\/h4>\n\n\n\n<p><strong>Git reset<\/strong> is&nbsp;a powerful command that is used to <strong>undo local changes<\/strong> and <strong>delete commits<\/strong> from repository depending on how it is invoked.<\/p>\n\n\n\n<p><strong>Undo Local Changes:<\/strong><\/p>\n\n\n\n<p>Let&#8217;s say, you have done changes (modified some files, added new files etc.)  in your local repository and you want to undo the changes.  You can do it by using following reset command-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard\n\n# Resetting particular file\ngit reset &lt;file-name><\/code><\/pre>\n\n\n\n<p>The <code>--hard<\/code> option will <strong>undo all the changes or modifications<\/strong> that you have done in files, and <strong>move files back to working area from staging area<\/strong>.<\/p>\n\n\n\n<p>If you want to keep the changes but remove files from staging area, use <code>--soft <\/code>flag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft<\/code><\/pre>\n\n\n\n<p><strong>Deleting Commits:<\/strong><\/p>\n\n\n\n<p>If we do git reset to a particular commit, it will move the HEAD pointer to that commit and delete all the commits above in the git history.<\/p>\n\n\n\n<p>Let&#8217;s consider the following sample git commit history. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline\n\n# Output:\n5986dd4 (HEAD -> dev, origin\/dev) Modify invalid login message\n8ff6ad5 Typo fix\nbb80604 CSS fix\nc64b176 Modify login page design \n0c633c0 Create SignUp Page\n0bad73e Login Validation\n052e288 Implement Login<\/code><\/pre>\n\n\n\n<p>We have couple of commits in the dev branch. The HEAD is currently pointing to the latest commit (5986dd4). <\/p>\n\n\n\n<p>Now, suppose you want to undo the  last commit. Following command will serve our purpose-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard &lt;commit-hash>\n\n# Example:\ngit reset --hard 8ff6ad5\n\n# Latest commit can also be deleted using Head\ngit reset --hard Head~1\n<\/code><\/pre>\n\n\n\n<p>`After resetting, let&#8217;s check the git commit history:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline\n\n# Output:\n8ff6ad5 (HEAD -> dev, origin\/dev) Typo fix\nbb80604 CSS fix\nc64b176 Modify login page design \n0c633c0 Create SignUp Page\n0bad73e Login Validation\n052e288 Implement Login<\/code><\/pre>\n\n\n\n<p>As you can see, the latest commit <em>(5986dd4) &#8211; Modify invalid login message<\/em>, has been deleted and there is no trace\/record in git log. Now the HEAD is pointing to commit (8ff6ad5), where we did reset.<\/p>\n\n\n\n<p>Let&#8217;s try to reset to <em>0bad73e Login Validation<\/em> commit and check git log.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard 8ff6ad5\ngit log --oneline\n\n# Output:\n0bad73e- (HEAD -> dev, origin\/dev) Login Validation\n052e288 Implement Login\n<\/code><\/pre>\n\n\n\n<p>Oops! What did you notice. All the commits above  <em>0bad73e<\/em> has been deleted. This is why we should not perform git reset for the commits which has been pushed to remote repository. It should be done in local repository only, if needed. <\/p>\n\n\n\n<p>To revert commits pushed to remote repository we should always use <code>git revert <\/code>command,<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Never perform git reset in remote repository commits, it might result in data loss.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">Git Revert<\/h4>\n\n\n\n<p>Git revert works a little different way compared to git reset. If we revert a particular commit, it will undo all the changes associated with that particular commit and it is recorded in the repository. It will add a new commit as &#8211; Revert &lt;Reverted Commit Message&gt; in the git commit history.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline\n\n# Output:\n8ff6ad5 (HEAD -> dev, origin\/dev) Typo fix\nbb80604 CSS fix\nc64b176 Modify login page design \n0c633c0 Create SignUp Page\n0bad73e Login Validation\n052e288 Implement Login<\/code><\/pre>\n\n\n\n<p>Let&#8217;s revert the 8ff6ad5 commit and check log. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert bb80604 \ngit log --oneline\n\n# Output:\n8sr6ad5 (HEAD -> dev, origin\/dev) Revert \"CSS fix\" \n8ff6ad5 Typo fix\nbb80604 CSS fix\nc64b176 Modify login page design \n0c633c0 Create SignUp Page\n0bad73e Login Validation\n052e288 Implement Login<\/code><\/pre>\n\n\n\n<p>As we can see in the log, an additional commit has been performed by git and all changes associated with the commit has been undone. <\/p>\n\n\n\n<p>In this way we can safely revert commits which has been already pushed to remote repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>So, we can conclude that&nbsp;<strong>git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch<\/strong> <strong>(i.e. your working directory)<\/strong>. You can also think of git revert as a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will explain the usage of two important git commands git reset and git revert. Both the commands are used to undo changes or commits, but there&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266,265],"tags":[278,279],"class_list":["post-1282","post","type-post","status-publish","format-standard","hentry","category-git","category-vcs","tag-git-reset","tag-git-revert","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1282"}],"version-history":[{"count":20,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1282\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1282\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}