Git Revert: Undo a Commit Without Rewriting History
Add an inverse change
Revert records a new commit that reverses the selected commit’s change. It does not erase the selected commit. Prefer it for a bad ordinary commit already shared with others. In the revert lab, A records Tea and B replaces it with Milk; the tree is clean.
git status --shortgit show --stat HEADgit revert --no-edit HEADgit log --oneline -3cat notes.txtA new commit C has parent B and restores Tea in this simple case. B remains in history. The generated message is accepted by --no-edit.
Preserve unrelated later changes
In a separate revert-earlier fixture, A contains Tea, B changes notes.txt to Milk, and C adds README.md without touching notes. Revert B by selecting the parent of current HEAD. A clean revert can preserve unrelated later work; it does not restore the whole project to A.
git revert --no-edit HEAD~1git show HEAD:notes.txtgit show HEAD:README.mdgit status --shortTea returns and README.md remains. Verify both contents and the project tests before sharing.
If an inverse change conflicts
In revert-conflict, later work has changed the same line again (Coffee). Reverting B can conflict. Inspect the intended result instead of blindly selecting one side. To abandon the operation, run git revert --abort only while the revert is in progress. This discards resolution edits, so save useful edits separately first. Merge commits require a deliberate mainline-parent choice, covered later.
git revert --no-edit HEAD~1# After the expected conflict, run separately:git status --shortgit revert --abortcat notes.txtThe first command fails with a conflict in this fixture. Abort restores Coffee and the original clean state.
Practice
Draw A → B → C in chronological order and label which commit reverses B. Explain why B is still accessible.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Revert: Undo a Commit Without Rewriting History — knowledge check
Reason about undo operations, affected states and preservation of work.
What happens to the original commit after an ordinary revert?
Which operation best fits a bad ordinary shared commit?
Reverting B after unrelated C adds README normally does what?
How can you abandon a conflicting revert in progress?
Why inspect after revert?