• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Revert Merge Commit

Revert Merge Commit

Last updated on May 9, 2022 by Sal Ferrarello

Typically a Git commit has exactly one parent, however a Git merge commit has two parents (see A Commit with Two Parents). The problem this creates with Git revert, is it is unclear which commit we want to revert to.

*   154382e (HEAD -> main) Merge branch 'feat/d' into main
|\  
| * 163fe29 (feat/d) D
* | 5d408f9 (feat/c) C
|/  
* 2b3a38b B
* ec6a2c7 A

If you run git revert HEAD, does Git revert to 163fe29 or to 2b3a38b?

Revert to Before the Merge Commit

Literally every time I have reverted a Git Merge commit I want to go back to the commit before the merge commit (in this case 2b3a38b). To get this behavior we pass -m 1 to Git revert.

git revert -m 1 HEAD

After this command, your history will look something like

*   b35e2ac (HEAD -> main) Revert "Merge branch 'feat/d' into main"
*   154382e Merge branch 'feat/d' into main
|\  
| * 163fe29 (feat/d) D
* | 5d408f9 (feat/c) C
|/  
* 2b3a38b B
* ec6a2c7 A

The state of HEAD (b35e2ac) will now be identical to before the merge commit (2b3a38b B).

When working with Git and you want to revert a merge commit, you need to indicate which parent commit you want to revert to.

Further Reading on Reverting Merge Commits

  • How to revert a merge commit that’s already pushed to remote branch?
  • See the How to Undo a Pushed Merge section on How to Undo a Merge in Git
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Computing, Dev Tips, Solution Tagged With: Git, revert

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in