Kshlerin WebStudio 🚀

Undo a merge by pull request

September 19, 2026

Undo a merge by pull request

Mistakes happen, especially when collaborating on complex software projects. One common scenario developers face is the need to undo a merge by pull request. Perhaps the merged code introduced unexpected bugs, broke existing functionality, or simply wasn’t ready for prime time. Understanding how to safely and effectively revert a merge is a critical skill for any developer using Git and platforms like GitHub, GitLab, or Bitbucket. This guide will walk you through the various methods you can use to undo a merge, providing step-by-step instructions and best practices to minimize disruption and data loss. We’ll cover techniques ranging from simple reverts to more advanced strategies, ensuring you have the knowledge to handle any situation with confidence. This process might also be called reversing a pull request or backing out a merge.

Understanding the Implications of Undoing a Merge

Before diving into the technical details of how to undo a merge by pull request, it’s crucial to understand the potential implications of such an action. Merging code typically signifies that the changes have been reviewed and approved, and are considered ready to be integrated into the main codebase. Reverting a merge essentially rolls back this integration, potentially impacting other developers who have based their work on the merged code. It’s therefore essential to communicate clearly with your team before initiating a revert. According to a study by Atlassian, poor communication is a leading cause of project failure [ Atlassian Blog ].

Consider a scenario where a feature branch was merged into the main branch, and several developers have subsequently pulled the updated main branch and begun building new features on top of it. Undoing the merge in this case could lead to significant conflicts and rework for those developers. Therefore, always assess the impact of the revert and communicate clearly with your team before proceeding. Explore alternative solutions first, such as identifying and fixing the specific bugs introduced by the merged code, instead of completely reverting the entire merge. Often, a targeted hotfix is a better solution than a complete rollback. Discuss the options with your team to arrive at the best course of action.

Furthermore, remember that reverting a merge creates a new commit that effectively undoes the changes introduced by the original merge commit. This new commit becomes part of the project history, leaving a record of the revert. This is generally desirable, as it provides transparency and traceability. However, in some rare cases, you might want to completely remove the merge commit from the history. This is generally discouraged, as it can lead to significant problems if others have already based their work on that commit. Rewriting history should only be done with extreme caution and with the full understanding and consent of the entire team.

Methods to Undo a Merge

There are several ways to undo a merge by pull request, each with its own advantages and disadvantages. The most common and generally recommended approach is to use the git revert command. This command creates a new commit that undoes the changes introduced by the specified merge commit. This approach is safe and non-destructive, as it doesn’t modify the existing history.

Here’s how to use the git revert command:

  1. First, identify the commit hash of the merge commit you want to undo. You can find this in your Git history using git log.
  2. Then, run the command git revert -m 1 . The -m 1 option specifies that you want to revert the first parent of the merge commit (usually the main branch). If you need to revert the other parent, use -m 2.
  3. Git will then open a text editor with a pre-populated commit message. Review the message and modify it as needed to accurately describe the revert.
  4. Save the commit message and close the editor. Git will then create a new commit that undoes the changes from the original merge.
  5. Finally, push the revert commit to the remote repository using git push.

Another approach is to use the git reset command. However, this method is generally discouraged, as it modifies the existing history and can lead to significant problems if others have already based their work on the merged code. git reset is best used only when you are working on a local branch that hasn’t been pushed to the remote repository. You can use the command to move your branch to the point just before the merge. Here’s an example:

The git reset command can be used in several ways, including:

  • git reset –soft : This moves the branch pointer to the specified commit, but leaves the changes in the staging area.
  • git reset –mixed : This moves the branch pointer to the specified commit and unstages the changes, but leaves them in your working directory.
  • git reset –hard : This moves the branch pointer to the specified commit and discards all changes in your working directory. This is the most dangerous option and should only be used with extreme caution.

The featured snippet paragraph: Using git revert with the -m 1 option is the safest and recommended way to undo a merge. The -m 1 flag specifies which parent of the merge commit should be considered the “mainline” for the revert. Typically, this is the branch you merged into (e.g., main or develop). By specifying the correct parent, Git can accurately determine how to undo the changes introduced by the merge.

Best Practices for Reverting Merges

When you need to undo a merge by pull request, following best practices can help minimize disruption and ensure a smooth process. First and foremost, communicate with your team. As mentioned earlier, reverting a merge can impact other developers, so it’s essential to let them know what you’re doing and why. Explain the reason for the revert and any potential consequences. This will help prevent confusion and ensure that everyone is on the same page. A clear and concise explanation in the revert commit message is also helpful. For example, “Reverting merge commit abc123 due to critical bug introduced in feature X. See issue 456 for details.”

Before initiating a revert, carefully consider the impact of the revert on other branches and environments. If the merged code has already been deployed to production, reverting the merge may not be the best solution. In such cases, it may be better to identify and fix the specific bugs introduced by the merged code, and then deploy a hotfix. This will minimize the disruption to users and avoid the need to completely roll back the changes. According to a survey by DORA, high-performing teams deploy code more frequently and have lower change failure rates [ Google Cloud DevOps Research and Assessment (DORA) ].

After reverting a merge, it’s important to thoroughly test the codebase to ensure that the revert has not introduced any new issues. Run all relevant unit tests, integration tests, and end-to-end tests. Also, consider performing manual testing to verify that the application is functioning as expected. If you find any issues, fix them promptly and deploy a new version of the code. Furthermore, after the root cause of the problematic merge is identified and fixed, consider re-integrating the changes with a new pull request after thorough testing. This ensures that valuable features are not permanently lost due to an initial faulty merge.

Advanced Scenarios and Considerations

In some cases, undoing a merge by pull request can be more complex than simply running git revert. For example, if the merge commit has been followed by other merges or commits, reverting the merge may create conflicts that need to be resolved. In such cases, you may need to manually resolve the conflicts and then commit the changes. This can be a time-consuming and error-prone process, so it’s important to proceed with caution.

Another challenging scenario is when you need to revert a merge that has already been reverted. This can happen if the initial revert was not successful or if new issues have been discovered since the revert. In such cases, you may need to revert the revert commit and then re-revert the original merge commit. This can quickly become confusing, so it’s important to carefully track the changes and ensure that you are reverting the correct commits. Consider using Git’s interactive rebase feature to visualize and manipulate the commit history.

In rare cases, you may need to completely remove the merge commit from the history. This is generally discouraged, as it can lead to significant problems if others have already based their work on that commit. However, if you are absolutely certain that no one has based their work on the commit, you can use the git filter-branch command to rewrite the history and remove the commit. Be aware that this is a destructive operation and should only be done with extreme caution. Always back up your repository before attempting to rewrite the history. More information about using git filter-branch can be found on the Git documentation page [ Git Documentation ].

Infographic here
FAQ About Undoing a Merge -------------------------
**Q: What is the safest way to undo a merge?**
A: The safest way is using `git revert -m 1 `. This creates a new commit that undoes the changes without altering history.
**Q: Why do I need the -m 1 option with git revert?**
A: The `-m 1` option specifies which parent of the merge commit should be considered the "mainline" for the revert. This is usually the branch you merged into.
**Q: What should I do if git revert creates conflicts?**
A: You'll need to manually resolve the conflicts in your working directory, stage the changes, and then create a new commit with the resolved conflicts.
**Q: Is it ever okay to use git reset to undo a merge?**
A: Generally, no. `git reset` modifies history and should only be used on local branches that haven't been pushed to the remote repository. Using `git reset` after a push will create a lot of problems.
**Q: What if I need to undo a revert?**
A: You can revert the revert commit itself, effectively re-applying the original merge. Then, you might need to re-revert if needed.
Key takeaways:
  • Always communicate with your team before reverting a merge.
  • Use git revert as the preferred method to undo a merge.
  • Thoroughly test after reverting to ensure stability.

By understanding the implications, methods, and best practices outlined above, you can confidently undo a merge by pull request when necessary, minimizing disruption and ensuring the integrity of your codebase. Remember, clear communication and careful planning are essential for a successful revert. This process is sometimes referred to as a reverse merge or a pull request rollback. Further, consider exploring branching strategies that can minimize the impact of faulty merges. Learn more about effective branching strategies here.

Now that you’re equipped with the knowledge to handle merge reversals, practice these techniques in a safe environment. Experiment with different scenarios, resolve conflicts, and get comfortable with the process. The next time you need to undo a merge, you’ll be ready to tackle it with confidence and expertise. Consider reading more on topics like Git branching strategies, resolving merge conflicts, and continuous integration workflows to further enhance your skills and prevent future issues.

Question & Answer :
Someone accepted a pull request which they shouldn’t have. Now we have a bunch of broken code merged in. How do you undo a pull request? I was just going to revert the changes to the commit just before the merge, but I noticed that it merged in a bunch of commits. So now there are all these commits from this person from days before the merge. How do you undo this?

There is a better answer to this problem, though I could just break this down step-by-step.

You will need to fetch and checkout the latest upstream changes like so, e.g.:

git fetch upstream git checkout upstream/master -b revert/john/foo_and_bar 

Taking a look at the commit log, you should find something similar to this:

commit b76a5f1f5d3b323679e466a1a1d5f93c8828b269 Merge: 9271e6e a507888 Author: Tim Tom <<a class="__cf_email__" data-cfemail="4e3a27230e3a2123602d2123" href="/cdn-cgi/l/email-protection">[email protected]</a>> Date: Mon Apr 29 06:12:38 2013 -0700 Merge pull request #123 from john/foo_and_bar Add foo and bar commit a507888e9fcc9e08b658c0b25414d1aeb1eef45e Author: John Doe <<a class="__cf_email__" data-cfemail="157f7a7d7b55717a703b767a78" href="/cdn-cgi/l/email-protection">[email protected]</a>> Date: Mon Apr 29 12:13:29 2013 +0000 Add bar commit 470ee0f407198057d5cb1d6427bb8371eab6157e Author: John Doe <<a class="__cf_email__" data-cfemail="b2d8dddadcf2d6ddd79cd1dddf" href="/cdn-cgi/l/email-protection">[email protected]</a>> Date: Mon Apr 29 10:29:10 2013 +0000 Add foo 

Now you want to revert the entire pull request with the ability to unrevert later. To do so, you will need to take the ID of the merge commit.

In the above example the merge commit is the top one where it says “Merged pull request #123…”.

Do this to revert the both changes (“Add bar” and “Add foo”) and you will end up with in one commit reverting the entire pull request which you can unrevert later on and keep the history of changes clean:

git revert -m 1 b76a5f1f5d3b323679e466a1a1d5f93c8828b269