With SVN it is easy to reverse-merge a commit, but how to do that with Git?
-
1Duplicate of [Revert to a previous Git commit](http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit) – Jul 16 '14 at 21:55
-
1I don't think this is what is meant by the question. A reverse-merge in SVN also reverts the changes from the commit to local changes, so you can edit them and commit again. – Dormouse Sep 26 '16 at 10:53
-
1Possible duplicate of [How to revert a merge commit that's already pushed to remote branch?](https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch) – Sebastian Patten Nov 26 '19 at 16:44
6 Answers
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

- 349,597
- 67
- 533
- 578

- 1,949
- 1
- 12
- 13
-
40How can you get the 'number' associated with the parent? Do branches have an intrinsic numerical 'id'? – daniyalzade Sep 05 '11 at 20:55
-
10Use `git show
` to see the parents, the numbering is the order they appear e.g. `Merge: e4c54b3 4725ad2` – supermethod Dec 10 '12 at 11:08 -
9example: `git revert -m 1 SHA1` That command worked for me to revert a merge commit that was several merge commits prior to head and had many commits underneath. – c.apolzon Dec 13 '12 at 22:19
-
4
-
8This still doesn't provide enough info about how the results of git show relate to parent numbers. Is the "order they appear" 1-based? 0-based? In this example specifically, what is the SHA1 of parent 1? e4c54b3 or 4725ad2? – cowlinator Dec 05 '17 at 20:33
-
7@cowlinator running `git help revert` and looking at the `-m` flag says: _This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent._ Although be careful b/c it also says: _Reverting a merge commit declares that you will **never** want the tree changes brought in by the merge. As a result, later merges will **only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge.**_ – BigHeadCreations Oct 24 '18 at 16:51
-
your comment giving the example needs to be explained better. Not easy to go from parent "1" to that little mini example. Which is the parents?e4c54b3 or 4725ad2? I mean, it's just unnecessary to leave ambiguous comments I don't know why developers do it. – Philip Aug 09 '22 at 20:38
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

- 9,483
- 2
- 50
- 58

- 121,135
- 26
- 193
- 155
-
36You would probably need to supply `-m
` option to `git revert` to specify which change to revert. If you want to undo a merge of non-published history, use `git reset --hard HEAD^1`. – Jakub Narębski Nov 27 '09 at 21:46 -
3Jakub: that's true if you are reverting a merge commit, but in Subversion terminology, "reverse-merge" is actually simply the name for reverting any kind of commit. – Ben James Aug 17 '11 at 22:00
-
7Do note that using `-m` means a future merge from the un-merged branch will not include the changes from before that merge! See http://schacon.github.com/git/howto/revert-a-faulty-merge.txt for proper ways to re-merge an un-merged branch. – Martijn Pieters Dec 01 '11 at 14:32
-
2
-
updated link: https://schacon.github.io/git/howto/revert-a-faulty-merge.txt (user-controlled Github Pages were moved to github.io to avoid risks of sharing cookies with github.com) – Beni Cherniavsky-Paskin Jun 17 '22 at 06:50
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

- 4,323
- 1
- 23
- 34
git reset --hard HEAD^
Use the above command to revert merge changes.

- 19,824
- 17
- 99
- 186

- 81
- 1
-
5This throws away the merge commit, which doesn't do what the original poster is asking. The OP is trying to do a reverse patch to undo previous changes, not erase the history of the previous changes altogether. – Jul 16 '14 at 21:54
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.

- 39
- 3
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>

- 1
- 1