开发者

Git: how to reverse-merge a commit?

With SVN it is easy to reverse-merge a commit, but h开发者_开发百科ow to do that with Git?


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


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

$ git revert <commit-hash>

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).

If your previous commit is a merge commit you can run this command

$ git revert -m 1 <commit-hash>

See schacon.github.io/git/howto/revert-a-faulty-merge.txt for proper ways to re-merge an un-merged branch


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


git reset --hard HEAD^ 

Use the above command to revert merge changes.


git revert -m hash
-m means mainline.
A merge has two parents.
If you merge branch A to branch B, B is parent 1 and A is parent 2.
Basically you specify, I want to revert the commit hash, which changes based on parent B, then you say
$ git revert -m 1 hash.

If you git log, you will see the merge commit as

commit <hash>
Merge: <parent 1> <parent 2>
Author: Author <author@email.com>
Date:   Mon Oct 24 21:54:00 2022 +0000
    comment message

You check what the <parent 1> and <parent 2> are in the log, you will know which parent/base you want to revert this commit to.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜