How Do I Revert a Merge Commit That Has Already Been Pushed to Remote?

Better Stack Team
Updated on July 25, 2024

To revert a merge commit that has already been pushed to a remote repository, you'll need to follow a careful process to ensure you don't disrupt the work of other collaborators. Reverting a merge commit creates a new commit that undoes the changes introduced by the merge commit. Here's how you can do it:

1. Identify the Merge Commit to Revert

First, identify the merge commit that you want to revert. You'll need the commit hash or a reference to it.

 
git log --oneline --graph --decorate

This command will display the commit history, including merge commits. Note down the commit hash of the merge commit you want to revert.

2. Revert the Merge Commit Locally

Next, revert the merge commit locally on your machine:

 
git revert -m 1 <merge-commit-hash>

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).

3. Resolve Conflicts (if any)

If Git encounters conflicts during the revert process, it will pause and allow you to resolve them manually. Use git status to see which files have conflicts, resolve them using your preferred text editor or Git tool, stage the resolved files with git add, and then continue the revert process:

 
git revert --continue

Repeat this until all conflicts are resolved and the revert is complete.

4. Commit the Revert

Once the revert is successful, Git will create a new commit that undoes the changes from the merge commit.

5. Push the Revert Commit

Now, push the revert commit to the remote repository:

 
git push origin <your-branch>

Replace <your-branch> with the name of your branch where you reverted the merge commit.

Important Considerations:

  • Collaboration: Reverting a merge commit affects the project history. Communicate with your team if they are affected by the revert.
  • Do Not Rewrite History: Avoid using git push --force to push a rewritten history to remote branches that others are working on, as this can cause issues with their repositories.

Alternative Method: Revert Using git revert --no-commit

If you prefer, you can use git revert --no-commit to revert the merge commit without immediately committing the changes. This allows you to make additional modifications before finalizing the revert commit:

 
git revert -m 1 --no-commit <merge-commit-hash>

Followed by resolving conflicts if any, and then:

 
git commit -m "Revert merge commit <merge-commit-hash>"
git push origin <your-branch>

Summary

Reverting a merge commit in Git involves creating a new commit that undoes the changes introduced by the merge. This process ensures you maintain a clear history while correcting any unintended merges. Always exercise caution when rewriting history, especially when collaborating with others.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github