Git: how to reverse-merge a commit?

GitMerge

Git Problem Overview


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

Git Solutions


Solution 1 - 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

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

git reset --hard HEAD^ 

Use the above command to revert merge changes.

Solution 5 - Git

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMotView Question on Stackoverflow
Solution 1 - GitMarcus EricssonView Answer on Stackoverflow
Solution 2 - GitBen JamesView Answer on Stackoverflow
Solution 3 - GitRudedogView Answer on Stackoverflow
Solution 4 - Gitkapil chakravarthyView Answer on Stackoverflow
Solution 5 - GitrkrisztianView Answer on Stackoverflow