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
-
I 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
-
Possible 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
5 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
- 330,190
- 66
- 504
- 555
- 1,779
- 1
- 11
- 13
-
38How can you get the 'number' associated with the parent? Do branches have an intrinsic numerical 'id'? – daniyalzade Sep 05 '11 at 20:55
-
7Use `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
-
5This 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
-
3@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
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.com/git/howto/revert-a-faulty-merge.txt for proper ways to re-merge an un-merged branch
- 9,338
- 5
- 56
- 86
- 114,847
- 26
- 189
- 155
-
34You 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
-
6Do 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
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,034
- 1
- 20
- 30
git reset --hard HEAD^
Use the above command to revert merge changes.
- 19,471
- 17
- 87
- 173
- 71
- 1
-
3This 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
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