Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This depends a lot on what you mean by "revert".</p> <h2>Temporarily switch to a different commit</h2> <p>If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:</p> <pre class="lang-sh prettyprint-override"><code># This will detach your HEAD, that is, leave you with no branch checked out: git checkout 0d1d7fc32 </code></pre> <p>Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:</p> <pre><code>git checkout -b old-state 0d1d7fc32 </code></pre> <p>To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)</p> <h2>Hard delete unpublished commits</h2> <p>If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:</p> <pre class="lang-sh prettyprint-override"><code># This will destroy any local modifications. # Don't do it if you have uncommitted work you want to keep. git reset --hard 0d1d7fc32 # Alternatively, if there's work to keep: git stash git reset --hard 0d1d7fc32 git stash pop # This saves the modifications, then reapplies that patch after resetting. # You could get merge conflicts, if you've modified things which were # changed since the commit you reset to. </code></pre> <p>If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.</p> <h2>Undo published commits with new commits</h2> <p>On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.</p> <pre class="lang-sh prettyprint-override"><code># This will create three separate revert commits: git revert a867b4af 25eee4ca 0766c053 # It also takes ranges. This will revert the last two commits: git revert HEAD~2..HEAD #Similarly, you can revert a range of commits using commit hashes: git revert a867b4af..0766c053 # Reverting a merge commit git revert -m 1 &lt;merge_commit_sha&gt; # To get just one, you could use `rebase -i` to squash them afterwards # Or, you could do it manually (be sure to do this at top level of the repo) # get your index and work tree into the desired state, without changing HEAD: git checkout 0d1d7fc32 . # Then commit. Be sure and write a good message describing what you just did git commit </code></pre> <p>The <a href="http://schacon.github.com/git/git-revert.html" rel="noreferrer"><code>git-revert</code> manpage</a> actually covers a lot of this in its description. Another useful link is <a href="https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_undoing_merges" rel="noreferrer">this git-scm.com section discussing git-revert</a>.</p> <p>If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).</p> <p>You may also find this answer helpful in this case:<br> <a href="https://stackoverflow.com/questions/34519665/how-to-move-head-forward-checkout-revet-reflog-reset/34519716#34519716">How to move HEAD back to a previous location? (Detached head)</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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