Sub topic 12
Reverting a Merged Pull Request
This chapter assumes you know basic Git (commit, branch, push, pull), can read git log --graph output, and know the three merge methods from earlier chapters. You will learn how to undo a merged pull request for each method, and why the merge commit needs special handling.
What a revert does
git revert does not delete history. It creates a new commit that applies the inverse of an earlier commit. The original commits stay in the log, so the revert is safe on a shared branch like main.
The number of commits you must revert depends on how the pull request was merged.
| Merge method | What landed on main | What to revert |
|---|---|---|
| Merge commit | One merge commit with two parents, plus the branch commits | The merge commit, using `-m 1` |
| Squash and merge | One ordinary commit | That one commit |
| Rebase and merge | One new commit per original commit | Every commit, as a range |
Reverting a merge commit
A merge commit has two parents. The first parent is the base branch (main). The second is the tip of the feature branch. Git cannot tell which side you want to undo, so it refuses a plain revert and asks for a mainline with -m.
git switch main
git pull
git revert -m 1 <merge-commit-sha>
git push origin main-m 1 means "keep the first parent as the mainline". The new commit removes everything the feature branch brought in.
Reverting a squash merge
A squash merge leaves one ordinary commit on main, so no -m option is needed.
git revert <squash-commit-sha>This is the simplest case. One pull request is one commit, and one revert undoes it.
Reverting a rebase merge
A rebase merge copies each branch commit onto main with a new SHA. There is no single commit that represents the pull request. Revert the whole range, newest first.
git revert --no-commit <first-sha>^..<last-sha>
git commit -m "Revert PR #123"--no-commit applies all the inverses and lets you record them as one commit. Without it, Git creates one revert commit per original commit.
Using the revert button on GitHub
A merged pull request has a Revert button. It opens a new pull request that contains the revert commit. GitHub chooses the right mainline for a merge commit. You then review and merge that pull request like any other.
This works well for squash and merge commits. For a rebase merge, the button may not cover every commit, so check the file changes in the generated pull request before you merge it.
Reverting a revert
A revert is itself a commit. If you later want the feature back, reverting the revert is the cleanest way.
A common mistake comes from merge commits. After you revert a merge, Git treats the feature branch's commits as already merged. If you fix the branch and merge it again, the original changes will not come back, because Git sees them as already in main. Two ways to recover:
- Revert the revert commit, then add your fixes on top.
- Create a new branch with the fixes and open a fresh pull request.
Common errors
error: commit ... is a merge but no -m option was given: add-m 1to the command.- Conflicts during the revert: later commits changed the same lines. Resolve them as in a normal merge, then run
git revert --continue. - Re-merging a fixed branch brings nothing back: see the section above on reverting a revert.
Exercise
A pull request was merged with "Create a merge commit". You run git revert <merge-commit-sha> and Git reports an error. What is the fix, and why was the error raised?
Show answer
Run git revert -m 1 <merge-commit-sha>. A merge commit has two parents, so Git cannot know which side to treat as the mainline. -m 1 selects the first parent, which is the base branch, so the revert removes the changes the feature branch added.
Exercise
You reverted a merge commit, fixed the bug on the same feature branch, and merged it again. The original changes are missing from main. Why, and how do you recover?
Show answer
Git considers the feature branch's old commits already merged, so the second merge only brings in the new fix. Revert the earlier revert commit to restore the original changes, then merge or add your fix on top. Alternatively, open a new pull request from a new branch.