{"id":4031,"date":"2022-07-17T08:05:16","date_gmt":"2022-07-17T07:05:16","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=4031"},"modified":"2022-07-17T08:05:37","modified_gmt":"2022-07-17T07:05:37","slug":"undo-the-last-commit","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/","title":{"rendered":"Undo the last commit"},"content":{"rendered":"<p>As a software engineer or developer, I am pretty sure we often use git in our work on a daily basis. Many commits are committed to your local or remote Git repository daily. However, we find some lines of codes or files in the last commit that should not be committed because of many reasons. As a consequence, we need to undo the last commit and might want to keep the last changes to be amended before re-committing them. How to do so in git? This tutorial will give you some ways to achieve this work.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue 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-6a4fe8b0e5350\" 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: #999;color:#999\" 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: #999;color:#999\" 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-6a4fe8b0e5350\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#undo-the-last-commit-with-git-reset\" >Undo the last commit with git reset<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#using-git-reset-with-the-%e2%80%93soft-option\" >Using git reset with the &#8211;soft option<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#lets-play-an-example\" >Let&rsquo;s play an example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#using-git-reset-with-the-%e2%80%93hard-option\" >Using git reset with the &#8211;hard option<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#undo-the-last-commit-with-git-revert\" >Undo the last commit with git revert<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#lets-get-through-a-demonstration\" >Let&rsquo;s get through a demonstration<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.itersdesktop.com\/fr\/2022\/07\/17\/undo-the-last-commit\/#conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"undo-the-last-commit-with-git-reset\"><\/span>Undo the last commit with <span style=\"text-decoration: underline;\">git reset<\/span><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"using-git-reset-with-the-%e2%80%93soft-option\"><\/span>Using git reset with the &#8211;soft option<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This is the easiest way to undo or reset the last commit by executing the git reset command with the option &#8211;soft followed by the value HEAD~1 that indicates the entry of the last commit.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git reset --soft HEAD~1<\/pre>\n<p>If you are not familiar with this notation, HEAD~1 means that you want to reset the HEAD (i.e. the last commit) to one commit before in the log history. Notes: the HEAD pointer of a git repo is the current location that you see when running <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git log --oneline<\/code> as shown in the following snippet.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">af3c39a (HEAD -&gt; master, origin\/master, origin\/HEAD) Assignment 4 - Task 3\r\nbe9e034 Assignment 4 - Task 2\r\n7239be5 Assignment 4 - Task 1\r\n575358a Fix the missing class name\r\nce7d9d7 React Animation Components\r\n85cbd7a React Animations\r\n115fb34 Fetch Post Comment<\/pre>\n<p>In the snippet above, the last commit is the one having the message \u00ab\u00a0Assignment 4 &#8211; Task 3\u00a0\u00bb where we see the HEAD pointing to the branch named master. So if we undo all changes in it (i.e. the last commit), the HEAD will point to the commit hash be9e034 having the message \u00ab\u00a0Assignment 4- Task 2\u00a0\u00bb. Therefore, the git reset above has to use HEAD~1 as described.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"lets-play-an-example\"><\/span>Let&rsquo;s play an example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Getting back to the example above, let&rsquo;s see the current status:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">% git status\r\nOn branch master\r\nYour branch is up to date with 'origin\/master'.\r\n\r\nChanges not staged for commit:\r\n  (use \"git add &lt;file&gt;...\" to update what will be committed)\r\n  (use \"git restore &lt;file&gt;...\" to discard changes in working directory)\r\n    modified:   package-lock.json\r\n\r\nno changes added to commit (use \"git add\" and\/or \"git commit -a\")<\/pre>\n<p>Let&rsquo;s have a look at some last commits as part of the output of the command git log &#8211;oneline:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">af3c39a (HEAD -&gt; master, origin\/master, origin\/HEAD, dev) Assignment 4 - Task 3\r\nbe9e034 Assignment 4 - Task 2\r\n7239be5 Assignment 4 - Task 1\r\n575358a Fix the missing class name\r\nce7d9d7 React Animation Components\r\n85cbd7a React Animations\r\n115fb34 Fetch Post Comment<\/pre>\n<p>Now, we want to undo the last commit meaning that the HEAD will be moved back to one commit. The HEAD should be at the commit hash be9e034 after invoking the command git reset &#8211;soft HEAD~1. Let&rsquo;s see the output.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">be9e034 (HEAD -&gt; master) Assignment 4 - Task 2\r\n7239be5 Assignment 4 - Task 1\r\n575358a Fix the missing class name\r\nce7d9d7 React Animation Components\r\n85cbd7a React Animations\r\n115fb34 Fetch Post Comment<\/pre>\n<p>OK. That&rsquo;s exactly what we need. To make sure what the command has returned, we can check it with the command \u00ab\u00a0git status\u00a0\u00bb:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">% git status\r\nOn branch master\r\nYour branch is behind 'origin\/master' by 1 commit, and can be fast-forwarded.\r\n  (use \"git pull\" to update your local branch)\r\n\r\nChanges to be committed:\r\n  (use \"git restore --staged &lt;file&gt;...\" to unstage)\r\n    modified:   src\/components\/AboutComponent.js\r\n\r\nChanges not staged for commit:\r\n  (use \"git add &lt;file&gt;...\" to update what will be committed)\r\n  (use \"git restore &lt;file&gt;...\" to discard changes in working directory)\r\n    modified:   package-lock.json<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"using-git-reset-with-the-%e2%80%93hard-option\"><\/span>Using git reset with the &#8211;hard option<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In contrast, the &#8211;hard option will erase the changes on files of the last commit. That&rsquo;s why it is called this way as a hard reset. Be careful with this option because we cannot get back to the last updates. In other words, the last updates will definitely be lost.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"undo-the-last-commit-with-git-revert\"><\/span>Undo the last commit with <span style=\"text-decoration: underline;\">git revert<\/span><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The git revert command is slightly different from the git reset one because it will record a new commit with the changes introduced by reverting the last commit. In other words, the git revert command will delete the last commit in terms of undoing the last changes but <strong>won&rsquo;t remove the commit from the log history<\/strong>.<\/p>\n<blockquote><p><strong>Remember:<\/strong> git revert will introduce a new commit and won&rsquo;t remove the last commit from the git history while the git reset won&rsquo;t do those actions.<\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"lets-get-through-a-demonstration\"><\/span>Let&rsquo;s get through a demonstration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">tnguyen@C02D2A6YMD6N test % git log --oneline\r\n35a5e8b (HEAD -&gt; main) Create b file\r\n890026c Make the first commit<\/pre>\n<p>Now, we run git revert and get the result as the screenshot below.<\/p>\n<figure id=\"attachment_4033\" aria-describedby=\"caption-attachment-4033\" style=\"width: 876px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4033\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2022\/07\/git-revert.png\" alt=\"The result of running git revert\" width=\"876\" height=\"588\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2022\/07\/git-revert.png 876w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2022\/07\/git-revert-300x201.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2022\/07\/git-revert-768x516.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><figcaption id=\"caption-attachment-4033\" class=\"wp-caption-text\">The result of running git revert<\/figcaption><\/figure>\n<p>Combining the screenshot and the git log output, we understand that git has deleted the b.txt file, added the changes to the git index, and then opened the git default editor to allow us to provide more git messages with the default messages as we see in the image. If we save and close the vim editor, we will see three commits from the log.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">tnguyen@C02D2A6YMD6N test % git log --oneline\r\n6fdd3ba (HEAD -&gt; main) Revert \"Create b file\"\r\n35a5e8b Create b file\r\n890026c Make the first commit<\/pre>\n<p>&nbsp;<\/p>\n<h1><span class=\"ez-toc-section\" id=\"conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>In this tutorial, you have seen all the ways of\u00a0<strong>undoing the last commit<\/strong>\u00a0of your Git repository.<\/p>\n<p>You have learned about the \u201c<strong>git reset<\/strong>\u201d command and the different ways of executing it depending on what you want to keep or not. You also learnt about the difference between the \u00ab\u00a0<strong>git reset<\/strong>\u00a0\u00bb and the \u00ab\u00a0<strong>git revert<\/strong>\u00a0\u00bb command, the latter adding a new commit in order to revert the one from your repository.<\/p>\n<p>If you are curious about\u00a0<strong>Git\u00a0<\/strong>or about software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!<\/p>\n<p>If you like Git, you might like our other articles:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.itersdesktop.com\/2022\/06\/29\/create-or-update-a-commit-by-a-specified-author\/\">How to create or update a commit by a specified author<\/a>.<\/li>\n<li><a href=\"https:\/\/www.itersdesktop.com\/2022\/06\/29\/create-an-empty-commit\/\">How to create an empty commit<\/a>.<\/li>\n<li><a href=\"https:\/\/www.itersdesktop.com\/2020\/08\/27\/exceptions-in-a-gitignore-file\/\">How to add some exceptions in a .gitignore file<\/a>.<\/li>\n<li><a href=\"https:\/\/www.itersdesktop.com\/2020\/06\/26\/add-git-branch-name-to-bash-prompt\/\">How to add git branch name to terminal prompt<\/a>.<\/li>\n<li><a href=\"https:\/\/www.itersdesktop.com\/2020\/04\/20\/git-shows-files-come-along-a-specific-commit\/\">How to show files come long a specific commit<\/a>.<\/li>\n<\/ul>\n<hr \/>\n<p>All constructive comments are always welcomed!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a software engineer or developer, I am pretty sure we often use git in our work on a daily basis. Many commits are committed to your local or remote&hellip; <\/p>\n","protected":false},"author":2,"featured_media":4035,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[611],"tags":[610,823,727,592],"class_list":["post-4031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","tag-git","tag-git-reset","tag-hands-on-git-guides","tag-huong-dan-su-dung-git"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=4031"}],"version-history":[{"count":4,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4031\/revisions"}],"predecessor-version":[{"id":4037,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4031\/revisions\/4037"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/4035"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=4031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=4031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=4031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}