Sub topic 12
Reverting a merged pull request
This chapter covers how to undo a merged pull request on the base branch without rewriting history. It assumes you can use git revert at a basic level, read git log --graph output, and know the three merge methods from the earlier chapters. You also need permission to open pull requests against the base branch.
Why revert instead of reset
A revert adds a new commit that applies the inverse of an earlier commit. It leaves the original commit in place. The base branch is shared and usually protected, so you cannot rewrite it with git reset --hard and a force push. A revert is an ordinary fast-forward change, so every clone can pull it normally.
What you have to revert depends on the merge method, because each method leaves different commits on the base branch.
| Merge method | What landed on the base branch | What to revert |
|---|---|---|
| Create a merge commit | One merge commit with two parents, plus the branch's original commits | The merge commit, with `-m 1` |
| Squash and merge | One ordinary commit with one parent | That single commit |
| Rebase and merge | The branch's commits, rewritten with new SHAs, and no merge commit | Every one of those commits |
Step 1: Identify what was merged
Before you revert anything, look at the base branch and find the commits the pull request added.
git switch main
git pull
git log --oneline --graph --decorate -n 15A merge commit shows up as a node with two lines joining it. A squash commit is a single commit with a title like Add feature (#123). A rebase merge shows a run of commits with no join. On GitHub, the pull request page also shows the commit SHA in the "merged commit" line near the top.
Reverting a merge commit
A merge commit has two parents. Git does not know which side of the merge you want to undo, so it refuses to guess:
git revert abc1234error: commit abc1234 is a merge but no -m option was given.Use -m to name the parent that should be kept as the mainline. Parent 1 is the branch you merged into, which is main for a pull request merge. Parent 2 is the tip of the feature branch.
git revert -m 1 <merge-commit-sha>This creates a new commit that undoes everything the feature branch brought in, relative to main. You can check the parents before you run it:
git show --no-patch --format="%H%n%P" <merge-commit-sha>The second line lists the parent SHAs in order. The first one is the base branch.
If you use -m 2 by mistake, Git tries to undo the changes main made relative to the feature branch. That produces a large and wrong diff. Always read the diff of the revert before you push it.
The revert-the-revert gotcha
Reverting a merge commit undoes the changes, but Git still records that the feature branch's commits were merged. Suppose you fix the feature on the same branch, push new commits, and open a pull request again. Git sees the old commits as already part of main. Only your new fix commits come through. The rest of the feature stays missing.
To bring the original work back, first revert the revert:
git revert <revert-commit-sha>This restores the original changes. After that, merging the corrected branch brings in only the fixes. Alternatively, create a new branch with the corrected work and open a fresh pull request.
git log --oneline --graph --decorate main* 9f3c2a1 (HEAD -> main) Revert "Revert "Merge pull request #123 from team/feature""
* 5d8e7b4 Revert "Merge pull request #123 from team/feature"
* abc1234 Merge pull request #123 from team/feature
|\
| * 71c0aa2 Add validation
| * 3e44f10 Add feature skeleton
|/
* 0b12ee9 Previous work on mainThe history is noisy, but it is accurate, and nobody has to rewrite it.
Reverting a squash merge
A squash merge is the simplest case. The whole pull request is one commit with one parent, so a plain revert works:
git revert <squash-commit-sha>You do not need -m. The revert removes all the changes of the pull request at once, even if the original branch had twenty commits. Because the squash commit has no link to the branch's original commits, the revert-the-revert gotcha does not apply in the same way. If you merge a corrected version of the same branch later, Git compares file content, not commit ancestry, so you may hit conflicts against the reverted state. In most cases the cleaner approach is a new branch created from the current main with the corrected changes.
Reverting a rebase merge
A rebase merge puts each commit of the branch on the base branch with a new SHA. There is no single commit that represents the pull request, so you must revert all of them. Find the first and last commit of the range from git log, then revert the range without committing each one:
git revert --no-commit <first-sha>^..<last-sha>
git commit -m "Revert pull request #123"<first-sha>^..<last-sha> includes the first commit and everything up to the last. Git applies the reverts from newest to oldest, which avoids conflicts that come from undoing changes out of order. --no-commit stages the combined result so you can make one revert commit with a clear message.
Two cautions apply:
- Another pull request may have landed in the middle of the range. Run
git log --oneline <first-sha>^..<last-sha>first, and make sure every commit listed belongs to the pull request you are undoing. - If a revert in the range conflicts with later changes, Git stops. Resolve the conflict, run
git add, and thengit revert --continue.
Using the Revert button on GitHub
On a merged pull request, GitHub shows a Revert button near the bottom of the conversation. It creates a new branch, usually named revert-123-feature, and opens a pull request that reverts the merge. You review and merge that pull request like any other, so branch protection and required checks still apply.
This works well for merge commits and squash merges, where there is one commit to undo. GitHub's documentation describes the generated pull request as containing one revert of the merge commit from the original pull request. For a rebase merge there is no such commit, so check the contents of the generated pull request carefully. If the generated change does not cover the whole branch, use the command line approach above.
You can also do the same on the command line and open the pull request yourself:
git switch -c revert-pr-123 origin/main
git revert -m 1 <merge-commit-sha>
git push -u origin revert-pr-123
gh pr create --title "Revert PR #123" --body "Reverts #123 because of a failing deploy."Replace -m 1 with nothing if the original pull request was squash merged.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `commit ... is a merge but no -m option was given` | You ran a plain revert on a merge commit | Add `-m 1` |
| The revert diff removes unrelated work | You used `-m 2`, or reverted a range that included other pull requests | Abort with `git revert --abort` and recheck the parents or the range |
| Re-merging the branch brings in almost nothing | The branch's commits are already in `main`'s ancestry after a merge-commit revert | Revert the revert, or start a new branch |
| Push rejected on the revert branch | The base branch moved while you worked | `git fetch` and rebase your revert branch on `origin/main` |
| Conflicts during revert | Later commits changed the same lines | Resolve them by hand, then `git revert --continue` |
Summary of the decision
Always find the merge method first. Then run the matching command, read the diff, and open the revert as a pull request so it gets review and CI. If you expect to land the feature again, plan for it: revert the revert for a merge commit, or begin a new branch from the current main after a squash.
Exercise
A pull request was merged with "Create a merge commit". The merge is commit abc1234 on main, and it caused a production bug. You revert it with git revert -m 1 abc1234. Two days later the author fixes the bug on the same feature branch and opens a new pull request, but the merged result is missing most of the feature. Why, and what do you do?
Show answer
The original revert undid the file changes, but Git still treats the feature branch's old commits as merged, so only the new fix commits come through. Revert the revert commit with git revert <revert-sha> to restore the original changes, then merge the corrected branch. Starting a new branch from main with the full corrected change is also valid.
Exercise
A pull request with four commits was merged using "Rebase and merge". Which command sequence undoes it with one revert commit, and why is plain git revert on one SHA not enough?
Show answer
Run git revert --no-commit <first-sha>^..<last-sha> and then git commit -m "Revert pull request #123". A rebase merge leaves four separate commits and no merge commit, so reverting one SHA would undo only part of the pull request. First check with git log that the range contains only commits from that pull request.