{"id":1660,"date":"2022-02-01T08:44:45","date_gmt":"2022-02-01T07:44:45","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1660"},"modified":"2024-02-28T10:24:12","modified_gmt":"2024-02-28T09:24:12","slug":"how-to-undo-a-git-add","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/02\/01\/how-to-undo-a-git-add\/","title":{"rendered":"How to Undo a Git Add  &#038; Git commit"},"content":{"rendered":"\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">Undo a Git Add before commit<\/span><\/h4>\n\n\n\n<p>To undo&nbsp;<code><em><strong>git add<\/strong><\/em><\/code>&nbsp;before a <em><strong>commit<\/strong><\/em>, run &nbsp;<code><em><strong>git reset &lt;file&gt;<\/strong><\/em><\/code>&nbsp;or&nbsp;<em><strong><code>git reset<\/code>&nbsp;<\/strong><\/em>to unstage all changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ to unstage a specific file \ngit reset &lt;file&gt; \n\n\/\/or to unstage all changes\ngit reset <\/code><\/pre>\n\n\n\n<p>In older versions of Git, the commands were<em><strong>&nbsp;<code>git reset HEAD &lt;file&gt;<\/code><\/strong><\/em>&nbsp;and&nbsp;<code><em><strong>git reset HEAD<\/strong><\/em><\/code>&nbsp;respectively. This was changed in Git 1.8.2<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">Undo git commit before push<\/span><\/h4>\n\n\n\n<ul class=\"has-medium-font-size\"><li><strong>Undo commit and unstage all files<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset HEAD~;\n\n\/\/or \n\ngit reset --mixed HEAD^\r\n<\/code><\/pre>\n\n\n\n<p>This resets the HEAD to the previous commit but removes the changes from the staging area. However, our changes remain in the working directory.<\/p>\n\n\n\n<ul class=\"has-medium-font-size\"><li><strong>Undo commit and keep all files staged<\/strong><\/li><\/ul>\n\n\n\n<p>In case we just want to\u00a0<code>undo<\/code>\u00a0the\u00a0<code>commit<\/code>\u00a0and change nothing more, we can use<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~<\/code><\/pre>\n\n\n\n<p>This will reset the HEAD to the previous commit but keep our changes in the staging area.<\/p>\n\n\n\n<p>This is most often used to make a few changes to your latest commit and\/or fix your commit message. Leaves working tree as it was before<span class=\"has-inline-color has-vivid-red-color\">\u00a0<code>reset<\/code><\/span>.<br><\/p>\n\n\n\n<ul class=\"has-medium-font-size\"><li><strong>Undo the commit and completely remove all changes<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard HEAD~;<\/code><\/pre>\n\n\n\n<p>This resets the HEAD to the previous commit and discards all changes. Be careful with this command as our local changes will be permanently deleted.<\/p>\n\n\n\n<p><code><span class=\"has-inline-color has-vivid-red-color\">hard<\/span><\/code>\u00a0resets the index and working tree. Any\u00a0<span class=\"has-inline-color has-vivid-red-color\"><code>changes<\/code>\u00a0<\/span>to tracked files in the working tree since the previous commit\u00a0<code>are <span class=\"has-inline-color has-vivid-red-color\">discarded<\/span><\/code><\/p>\n\n\n\n<p><strong><em><span class=\"has-inline-color has-vivid-red-color\">PS: <\/span><\/em><\/strong><em>&nbsp;In case you just want to rewrite the&nbsp;<code>commit message<\/code>, you could use <strong>git &#8211;amend <\/strong>instead.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit --amend -m \"new message\"<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><a href=\"https:\/\/stackoverflow.com\/questions\/1186535\/how-do-i-modify-a-specific-commit\">How do I modify a specific commit?<\/a><\/h4>\n\n\n\n<p>Use&nbsp;<a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/rewriting-history\/git-rebase\"><code>git rebase<\/code><\/a>. For example, to modify commit&nbsp;<code>bbc643cd<\/code>, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git rebase --interactive 'bbc643cd^'<\/code><\/pre>\n\n\n\n<p>Please note the caret&nbsp;<code>^<\/code>&nbsp;at the end of the command, because you need actually to rebase back to&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/1955985\/\">the commit&nbsp;<em>before<\/em>&nbsp;the one you wish to modify<\/a>.<\/p>\n\n\n\n<p>In the default editor, modify&nbsp;<code>pick<\/code>&nbsp;to&nbsp;<code>edit<\/code>&nbsp;in the line mentioning&nbsp;<code>bbc643cd<\/code>.<\/p>\n\n\n\n<p>Save the file and exit. git will interpret and automatically execute the commands in the file. You will find yourself in the previous situation in which you just had created commit&nbsp;<code>bbc643cd<\/code>.<\/p>\n\n\n\n<p>At this point,&nbsp;<code>bbc643cd<\/code>&nbsp;is your last commit and you can&nbsp;<a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/rewriting-history#git-commit--amend\">easily amend it<\/a>. Make your changes and then commit them with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git commit --all --amend --no-edit<\/code><\/pre>\n\n\n\n<p>Use&nbsp;<a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/rewriting-history\/git-rebase\"><code>git rebase<\/code><\/a>. For example, to modify commit&nbsp;<code>bbc643cd<\/code>, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git rebase --interactive 'bbc643cd^'\n<\/code><\/pre>\n\n\n\n<p>Please note the caret&nbsp;<code>^<\/code>&nbsp;at the end of the command, because you need actually to rebase back to&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/1955985\/\">the commit&nbsp;<em>before<\/em>&nbsp;the one you wish to modify<\/a>.<\/p>\n\n\n\n<p>In the default editor, modify&nbsp;<code>pick<\/code>&nbsp;to&nbsp;<code>edit<\/code>&nbsp;in the line mentioning&nbsp;<code>bbc643cd<\/code>.<\/p>\n\n\n\n<p>Save the file and exit. git will interpret and automatically execute the commands in the file. You will find yourself in the previous situation in which you just had created commit&nbsp;<code>bbc643cd<\/code>.<\/p>\n\n\n\n<p>At this point,&nbsp;<code>bbc643cd<\/code>&nbsp;is your last commit and you can&nbsp;<a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/rewriting-history#git-commit--amend\">easily amend it<\/a>. Make your changes and then commit them with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git commit --all --amend --no-edit\n<\/code><\/pre>\n\n\n\n<p>After that, return back to the previous HEAD commit using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git rebase --continue<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Undo a Git Add before commit To undo&nbsp;git add&nbsp;before a commit, run &nbsp;git reset &lt;file&gt;&nbsp;or&nbsp;git reset&nbsp;to unstage all changes. In older versions of Git, the commands were&nbsp;git reset HEAD &lt;file&gt;&nbsp;and&nbsp;git reset HEAD&nbsp;respectively. This was changed in Git 1.8.2 Undo git commit before push Undo commit and unstage all files This resets the HEAD to the [&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":[486,489,459,458,488,461,487],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1660"}],"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=1660"}],"version-history":[{"count":8,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"predecessor-version":[{"id":3119,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions\/3119"}],"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=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}