pablocattaneo
11/20/2019 - 9:08 PM

How to revert a merge commit that's already pushed to remote branch?

Yes, absolutely — you can revert a problematic pull request (PR) from dev without rewriting history. Here's how:


✅ Option 1: Revert the Entire PR Using git revert -m

If the PR was merged using a merge commit (the default for GitHub/GitLab), you can revert the whole PR cleanly with:

git checkout dev
git pull
git log --oneline

Find the merge commit from the PR (it will look like: Merge pull request #123 ...), then run:

git revert -m 1 <merge-commit-hash>
  • -m 1 tells Git to treat parent #1 (usually the dev branch) as the mainline.
  • This creates a new commit that undoes all changes from the merged PR.

Then push the result:

git push origin dev

✅ Option 2: Revert Individual Commits (if not merged with a merge commit)

If the PR was squash merged or rebase merged, then it won’t have a merge commit. You’ll need to:

  1. Identify the commit(s) introduced by the PR.
  2. Revert them manually:
git revert <bad-commit-hash>
# Or for multiple:
git revert <start-hash>^..<end-hash>

Then push:

git push origin dev

🔄 What Happens After Reverting?

  • The bad changes are undone, but the history remains intact.
  • The PR remains marked as "merged", but its code is no longer present in dev.
  • You (or the original developer) can later fix the issues and create a new PR if needed.