In git revert -m, the -m option specifies the parent number. This is needed because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.

When you view a merge commit in the output of git log, you will see its parents listed on the line that begins with Merge: (the second line in this case):

commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <ben@example.com>
Date:   Wed Aug 17 22:49:41 2011 +0100

Merge branch 'gh-pages'

Conflicts:
    README

In this situation, git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert 8f937c6 -m 2 will reinstate the tree as it was in 7c6b236.

To better understand what you're about to revert do git diff <parent_commit> <commit_to_revert>, in this case:

git diff 8989ee0 8f937c6

and

git diff 7c6b236 8f937c6

However, it's very important you realize that in doing so

"...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. This may or may not be what you want. See the revert-a-faulty-merge How-To for more details." (git-merge man page).

Answer from Ben James on Stack Overflow
Top answer
1 of 16
2295

In git revert -m, the -m option specifies the parent number. This is needed because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.

When you view a merge commit in the output of git log, you will see its parents listed on the line that begins with Merge: (the second line in this case):

commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <ben@example.com>
Date:   Wed Aug 17 22:49:41 2011 +0100

Merge branch 'gh-pages'

Conflicts:
    README

In this situation, git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert 8f937c6 -m 2 will reinstate the tree as it was in 7c6b236.

To better understand what you're about to revert do git diff <parent_commit> <commit_to_revert>, in this case:

git diff 8989ee0 8f937c6

and

git diff 7c6b236 8f937c6

However, it's very important you realize that in doing so

"...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. This may or may not be what you want. See the revert-a-faulty-merge How-To for more details." (git-merge man page).

2 of 16
735

Here's a complete example:

git revert -m 1 <commit-hash> 
git push -u origin master

git revert ... commits your changes.

  • -m 1 indicates that you'd like to revert to the tree of the first parent prior to the merge, as stated by this answer.

  • <commit-hash> is the commit hash of the merge that you would like to revert.

git push ... pushes your changes to the remote branch.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ git โ€บ how-to-revert-a-pushed-merge-commit-in-git
How to Revert a Pushed Merge Commit in Git? - GeeksforGeeks
May 28, 2024 - In Git, merge commits are created when integrating changes from one branch into another. Sometimes, you may find yourself in a situation where you need to revert a merge commit that has already been pushed to a remote repository. Reverting a merge commit requires careful consideration to maintain the integrity of the project history.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ git-undo-merge-how-to-revert-the-last-merge-commit-in-git
Git Undo Merge โ€“ How to Revert the Last Merge Commit in Git
March 30, 2022 - To undo a merge with the --merge flag, run git reflog to see the hashes of commits, then run git reset --merge previous-commit: You can also use the HEAD keyword with the --merge flag by running git reset --merge HEAD~1:
๐ŸŒ
Git Tower
git-tower.com โ€บ learn โ€บ git faq โ€บ how to undo a merge in git
How to undo a merge in Git | Learn Version Control with Git
5 days ago - You can use the "git reset" command to quickly and safely undo a merge. If the merge has already been pushed to the remote repository, use "git revert" instead.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ git โ€บ how-to-revert-the-last-merge-commit-in-git
How To Revert The Last Merge Commit in Git? - GeeksforGeeks
June 21, 2024 - Note that this can overwrite the history on the remote repository, so ensure that this is acceptable for your workflow. ... Git Revert: Use git revert when you want to create a new commit that undoes the changes from the merge commit.
๐ŸŒ
Graphite
graphite.com โ€บ guides โ€บ how-to-revert-a-merge-in-git
How to revert a merge in Git - Graphite
Yes, you can safely revert a merge that was pushed to a shared repository. The git revert command creates a new commit that undoes the changes, so it doesn't rewrite history.
Find elsewhere
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-revert-merge-commit-that-has-already-been-pushed-to-remote
How Do I Revert a Merge Commit That Has Already Been Pushed to Remote? | Better Stack Community
Replace <merge-commit-hash> with the hash of the merge commit you identified. The -m 1 option specifies that you want to revert to the parent of the merge commit on the mainline (typically the branch you merged into).
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-undo-git-merge
How to undo git merge ยท CoreUI
January 15, 2026 - Use git revert -m 1 for pushed merges to maintain history. The -m 1 flag tells revert which parent to keep (usually the main branch). Always verify youโ€™re on the correct branch before undoing merges.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ git-revert-merge
Git Revert Merge Commit: A Guide With Examples | DataCamp
November 5, 2024 - Replacing <commit_hash> with the hash of the commit we want to go back to. I recommend only using git reset to undo a merge when the merge only happened locally and hasn't been pushed yet.
๐ŸŒ
Squash
squash.io โ€บ how-to-revert-a-pushed-merge-commit-in-git
How to Revert a Pushed Merge Commit in Git - Squash Labs
October 28, 2023 - By squashlabs, Last Updated: Oct. 28, 2023 ... First, you need to identify the merge commit that you want to revert. You can use the following command to view the commit history and find the merge commit: ... This command will show you a graphical representation of the commit history, including the merge commits. Look for the merge commit that you want to revert and make note of its commit hash. Related Article: How to Create and Checkout Git Remote Tags
๐ŸŒ
Wikihow
wikihow.com โ€บ computers and electronics โ€บ software โ€บ software testing โ€บ how to undo a merge in git: 2 simple step-by-step methods
How to Undo a Merge in Git: 2 Simple Step-by-Step Methods
December 11, 2025 - Using git revert is a better option for pushed merges because it won't erase the merge history and will make a new commit that undoes the changes. You can use git revert with a local merge as well, but git reset may be beneficial because you ...
๐ŸŒ
Christian Engvall
christianengvall.se โ€บ undo-pushed-merge-git
Undo a pushed merge with git | Christian Engvall
Update: Iโ€™ve made an easy extension to git that makes this command more easy to remember, i call it gitUndo. With that extension all you need run is: git undo pushed-merge <merge-commit...
๐ŸŒ
Medium
medium.com โ€บ @felixhhyeung โ€บ how-to-revert-a-pushed-git-merge-the-right-way-4fb0fd26f737
[Git] How to revert a pushed git merge | by Felix HH Yeung | Medium
October 7, 2022 - Lets say mainis your main branch ... the merge to main. ... You merged like this and pushed to main branch. git revert -m 1 [the merge commit hash] : Revert the merge commit at mainbranch....
๐ŸŒ
GitKraken
gitkraken.com โ€บ home โ€บ learn โ€บ problems & solutions โ€บ how do you undo a git merge?
How do you undo a Git merge? | Solutions to Git Problems
February 7, 2025 - You will then select Reset <branch ... changes you want to undo to your remote repository, right-click on the merge commit and select Revert commit from the context menu....
๐ŸŒ
Warp
warp.dev โ€บ terminus by warp โ€บ git โ€บ undo a git merge
How To Undo Merged Commits in Git Using git-reset And git-revert | Warp
June 27, 2024 - $ git reflog 644bbfe (HEAD -> master, ... commit: add log in index.js 459ad3a HEAD@{9}: commit (initial): add file index.js $ git reset --merge 42d4df9 ยท It is important to note that unlike git revert, git reset rewrites the ...
๐ŸŒ
Medium
medium.com โ€บ @zigrazor โ€บ git-undo-merge-the-final-guide-7492235499b6
Git Undo Merge: The Final Guide | by Zigrazor | Medium
November 14, 2025 - To undo a git merge, you need to find the commit ID of your last commit. Then, you need to use the git reset command to reset your repository to its state in that commit. There is no โ€œgit revert mergeโ€ command.
๐ŸŒ
Graphite
graphite.com โ€บ guides โ€บ git-revert-commit-after-pushing
git revert commit after pushing - Graphite
git revert creates a new commit that undoes the changes from a previous commit, preserving the commit history. git reset moves the branch pointer to a previous commit, effectively removing commits from the history.
๐ŸŒ
Continuously Merging
articles.mergify.com โ€บ how-to-undo-a-merge-in-git
How to Undo a Merge in Git
June 8, 2023 - Even better, once changes have been made, you can revert the revert itself by referencing its id in git revert <sha-of-revert>. All those operations create a new commit that you can push to your remote, as needed.