Git: how to reverse-merge a commit?

前端 未结 5 638
日久生厌
日久生厌 2020-12-07 11:46

With SVN it is easy to reverse-merge a commit, but how to do that with Git?

相关标签:
5条回答
  • 2020-12-07 12:05

    If I understand you correctly, you're talking about doing a

    svn merge -rn:n-1
    

    to back out of an earlier commit, in which case, you're probably looking for

    git revert
    
    0 讨论(0)
  • 2020-12-07 12:08

    To create a new commit that 'undoes' the changes of a past commit, use:

    $ git revert <commit>
    

    It's also possible to actually remove a commit from an arbitrary point in the past by rebasing and then resetting, but you really don't want to do that if you have already pushed your commits to another repository (or someone else has pulled from you).

    0 讨论(0)
  • 2020-12-07 12:09

    If you don't want to commit, or want to commit later (commit message will still be prepared for you, which you can also edit):

    git revert -n <commit>
    
    0 讨论(0)
  • 2020-12-07 12:16

    To revert a merge commit, you need to use: git revert -m <parent number>. So for example, to revert the recent most merge commit using the parent with number 1 you would use:

    git revert -m 1 HEAD
    

    To revert a merge commit before the last commit, you would do:

    git revert -m 1 HEAD^
    

    Use git show <merge commit SHA1> to see the parents, the numbering is the order they appear e.g. Merge: e4c54b3 4725ad2

    git merge documentation: http://schacon.github.com/git/git-merge.html

    git merge discussion (confusing but very detailed): http://schacon.github.com/git/howto/revert-a-faulty-merge.txt

    0 讨论(0)
  • 2020-12-07 12:19
    git reset --hard HEAD^ 
    

    Use the above command to revert merge changes.

    0 讨论(0)
提交回复
热议问题