{"id":3031,"date":"2023-11-29T21:34:09","date_gmt":"2023-11-29T20:34:09","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3031"},"modified":"2023-11-29T21:34:09","modified_gmt":"2023-11-29T20:34:09","slug":"git-revert-vs-git-reset","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/11\/29\/git-revert-vs-git-reset\/","title":{"rendered":"&#8220;Git revert&#8221; VS &#8220;Git reset&#8221;"},"content":{"rendered":"\n<p>The purpose of the\u00a0git revert\u00a0command is to remove all the changes a single commit made to our source code repository. For example, if a past commit added a file named\u00a0index.html\u00a0to the repo, a git revert on that commit will remove the\u00a0index.html\u00a0file from the repo.<\/p>\n\n\n\n<p>When we revert a Git commit, the changes from the targeted commit are removed from our local workspace. A new commit is also created to reflect the new state of our repository.<\/p>\n\n\n\n<p>Let&#8217;s walk through an example of how to revert a Git commit, and differentiate the\u00a0git reset\u00a0and\u00a0git revert\u00a0commands.<\/p>\n\n\n\n<h4>git revert<\/h4>\n\n\n\n<p>The syntax to revert a Git commit and undo unwanted changes is simple: We just need to issue the\u00a0git revert\u00a0command and provide the ID of the commit to undo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert 4945db2<\/code><\/pre>\n\n\n\n<p><em><span class=\"has-inline-color has-luminous-vivid-orange-color\">example:<\/span> <\/em>Let&#8217;s assume, we&#8217;ll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ touch alpha.html\r\n$ git add . &amp;&amp; git commit -m \"1st git commit: 1 file\"\r\n\r\n$ touch beta.html\r\n$ git add . &amp;&amp; git commit -m \"2nd git commit: 2 files\"\r\n\r\n$ touch charlie.html\r\n$ git add . &amp;&amp; git commit -m \"3rd git commit: 3 files\"\r\n\r\n$ touch delta.html\r\n$ git add . &amp;&amp; git commit -m \"4th git commit: 4 files\"\r\n\r\n$ touch edison.html\r\n$ git add . &amp;&amp; git commit -m \"5th git commit: 5 files\"<\/code><\/pre>\n\n\n\n<p>A quick directory listing with the &#8220;<strong>ls command<\/strong>&#8221; shows five files in the current folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls\r\nalpha.html  beta.html  charlie.html delta.html  edison.html<\/code><\/pre>\n\n\n\n<p>A call to the &#8220;<strong>git reflog command<\/strong>&#8221; will show us our current commit history:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reflog\r\n(HEAD -> master)\r\nd846aa8 HEAD@{0}: commit: 5th git commit: 5 files\r\n0c59891 HEAD@{1}: commit: 4th git commit: 4 files\r\n4945db2 HEAD@{2}: commit: 3rd git commit: 3 files\r\ndefc4eb HEAD@{3}: commit: 2nd git commit: 2 files\r\n2938ee3 HEAD@{4}: commit: 1st git commit: 1 file<\/code><\/pre>\n\n\n\n<p>what would happen If we did a\u00a0git revert\u00a0on the third commit with ID 4945db2 ? (This was the git commit that added the\u00a0charlie.html\u00a0file)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert 4945db2<\/code><\/pre>\n\n\n\n<p>\u00a0this\u00a0git revert\u00a0example will leave four files in the local workspace and remove only the\u00a0charlie.html\u00a0file<\/p>\n\n\n\n<p><strong><em>PS:<\/em><\/strong> The\u00a0git revert\u00a0command will undo only the changes associated with a specific commit. In this\u00a0git revert\u00a0example, the third commit added the\u00a0charlie.html\u00a0file.\u00a0So, \u00a0the only file removed from our repository is\u00a0charlie.html.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls\r\nalpha.html  beta.html  delta.html  edison.html<\/code><\/pre>\n\n\n\n<p><strong><em>PS: <\/em><\/strong>We should also notice that when we\u00a0git revert\u00a0a commit, the reverted commit is deleted from our local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository&#8217;s history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.<\/p>\n\n\n\n<h4>git reset<\/h4>\n\n\n\n<p>When we\u00a0git revert\u00a0a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren&#8217;t affected. If we wish to undo every change since a given commit occurred, we&#8217;d want to issue a\u00a0 hard <strong><em>git reset<\/em><\/strong>, not revert .<\/p>\n\n\n\n<p>The\u00a0git reset\u00a0and\u00a0revert\u00a0commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the\u00a0git revert\u00a0command. To undo every change\u00a0that has happened since a given commit occurred, we should use\u00a0git reset<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Referebce:<\/h5>\n\n\n\n<p><a href=\"https:\/\/www.theserverside.com\/\">https:\/\/www.theserverside.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The purpose of the\u00a0git revert\u00a0command is to remove all the changes a single commit made to our source code repository. For example, if a past commit added a file named\u00a0index.html\u00a0to the repo, a git revert on that commit will remove the\u00a0index.html\u00a0file from the repo. When we revert a Git commit, the changes from the targeted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[459,455,461,775,774],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3031"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3031"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3031\/revisions"}],"predecessor-version":[{"id":3032,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3031\/revisions\/3032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}