Sub topic 9
Undoing a commit by adding a new one with git revert
This chapter assumes you can commit, push, and read git log, and that you know a branch is a pointer to a commit. It also assumes you have read the earlier chapters on the three areas and on git reset.
What revert does
git revert undoes a commit by adding a new commit. It works out the changes the target commit introduced, applies the opposite changes, and records the result as a normal commit. The original commit stays in the history.
Because nothing is removed, the branch only moves forward. That makes revert the safe choice for commits that others may already have pulled. reset moves the branch pointer backward, and anyone who already has the old commits is left out of sync.
Reverting a single commit
Find the commit with git log --oneline, then revert it by its hash. This example uses a1b2c3d.
git revert a1b2c3d
git push origin mainGit opens your editor with a default message such as Revert "Add broken parser". Save it, and the new commit is created. The push is an ordinary fast-forward push, so no force is needed.
To skip the editor, add --no-edit:
git revert --no-edit a1b2c3dWorking tree requirements and conflicts
git revert needs a clean working tree for the files it touches. Commit or stash your edits first.
If later commits changed the same lines, Git cannot apply the reverse change automatically. It stops with a conflict:
- Run
git statusto see the conflicted files. - Edit each file to the state you want and remove the conflict markers.
- Run
git addon each resolved file. - Run
git revert --continue.
To give up, run git revert --abort. The branch returns to where it was before you started.
Reverting several commits
You can pass more than one commit, or a range. Git creates one revert commit per original commit, applied from newest to oldest.
git revert --no-edit HEAD~2..HEADThis reverts the last two commits. To combine the reversals into a single commit, add -n (--no-commit). Git applies the changes to the working tree and index, and you then commit once.
git revert -n HEAD~2..HEAD
git commit -m "Revert the experimental search changes"Reverting a merge commit
A merge commit has two parents, so Git cannot tell which side to undo. You must name the mainline parent with -m. Parent 1 is normally the branch you merged into.
git revert -m 1 <merge-commit-hash>Without -m, Git refuses with an error saying the commit is a merge and no -m option was given.
Common mistakes
- Using
resetplus a force push on a shared branch whenrevertwould have worked. - Reverting a merge without
-m 1, or using the wrong parent number and undoing the wrong side. - Running
revertwith uncommitted edits in the files involved. - Expecting the reverted commit to disappear from
git log. It stays, and so does the revert commit.
Undoing a revert
A revert commit is a normal commit, so you can revert it too. This reapplies the original changes and is the standard way to restore reverted work.
git revert <revert-commit-hash>Exercise
A teammate has already pulled your latest commits on main. Commit e4f5a6b broke the build. Which command undoes it without forcing a push, and what happens to e4f5a6b in the history?
Show answer
Run git revert e4f5a6b and then git push origin main. Git adds a new commit that reverses the changes of e4f5a6b. The original commit stays in the history, so the push is a normal fast-forward and your teammate can pull it without trouble.
Exercise
You run git revert 9c8d7e6 and Git refuses because the commit is a merge. What do you add to the command, and which parent does the number refer to?
Show answer
Add -m 1, giving git revert -m 1 9c8d7e6. The number 1 selects the first parent as the mainline, which is usually the branch that received the merge. Git then reverses the changes brought in by the other parent.