Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>Undo a commit and redo</h3> <pre class="lang-sh prettyprint-override"><code>$ git commit -m "Something terribly misguided" # (1) $ git reset HEAD~ # (2) &lt;&lt; edit files as necessary &gt;&gt; # (3) $ git add ... # (4) $ git commit -c ORIG_HEAD # (5) </code></pre> <ol> <li>This is what you want to undo</li> <li>This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in <code>git status</code>, and you'll need to add them again before committing). If you <em>only</em> want to <em>add</em> more changes to the previous commit, or change the commit message<sup>1</sup>, you could use <code>git reset --soft HEAD~</code> instead, which is like <code>git reset HEAD~</code> (where <code>HEAD~</code> is the same as <code>HEAD~1</code>) but leaves your existing changes staged.</li> <li>Make corrections to working tree files.</li> <li><code>git add</code> anything that you want to include in your new commit.</li> <li>Commit the changes, reusing the old commit message. <code>reset</code> copied the old head to <code>.git/ORIG_HEAD</code>; <code>commit</code> with <code>-c ORIG_HEAD</code> will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the <code>-C</code> option.</li> </ol> <p>Beware however that if you have added any new changes to the index, using <code>commit --amend</code> will add them to your previous commit.</p> <p>If the code is already pushed to your server and you have permissions to overwrite history (rebase) then:</p> <pre><code>git push origin master --force </code></pre> <p>You can also look at this answer: </p> <p><a href="https://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location-detached-head/34519716#34519716">How to move HEAD back to a previous location? (Detached head)</a></p> <p>The above answer will show you <code>git reflog</code> which is used to find out what is the SHA-1 which you wish to revert to. Once you found the point to which you wish to undo to use the sequence of commands as explained above.</p> <hr> <p><sup>1</sup> Note, however, that you don't need to reset to an earlier commit if you just made a mistake in your <em>commit message</em>. The easier option is to <code>git reset</code> (to upstage any changes you've made since) and then <a href="https://stackoverflow.com/q/179123/1146608"><code>git commit --amend</code></a>, which will open your default commit message editor pre-populated with the last commit message. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload