How to Revert a Merged Pull Request in Git
Safely revert an accidentally merged Pull Request or merge commit using git revert with parent specification.
git revert -m 1 <merge-commit-hash>Step-by-Step Execution Guide
git log --oneline -n 5Look for the commit that says "Merge pull request #..." and copy its hash.
git revert -m 1 <merge-hash>-m 1 tells Git that parent 1 (the main branch you merged into) should be preserved as the mainline.
git push origin mainDeploys the rollback commit cleanly without rewriting shared branch history.
Critical Pitfalls & Precautions
- β’If you later want to re-merge the reverted branch, you must first revert the revert commit, otherwise Git considers those changes already incorporated.
Revert a Merge Commit - Frequently Asked Questions
Common questions about safe rollback, remote history implications, and troubleshooting.
A merge commit has two parents. Git needs to know which parent branch to keep as the baseline, which is specified by "-m 1".
Related Git Command Guides
Browse All Git RecipesUndo your most recent Git commit while keeping all your edited files staged or unstaged in your working directory.
Discard all unstaged and uncommitted file modifications and restore your working tree to the latest commit.
Rename your current local branch and update the upstream remote tracking branch on GitHub or GitLab.
Delete a branch from remote origin (GitHub, GitLab, Bitbucket) after a pull request has been merged.
Amend the commit message of your latest commit before or after pushing.
Remove files from the Git staging area (index) without losing any of your local changes.