Undoing and recovery
Revert a shared commit
Undo a published change by adding a new commit instead of rewriting shared history.
8 minute lesson
When a bad commit is already shared, preserve history and add a new commit that reverses its changes.
Start with a clean working tree. Inspect the target commit before changing anything:
git status
git show --stat 4f72a1c
Then revert it:
git revert 4f72a1c
Git applies the inverse patch and opens a commit message. The original commit remains in history, and the current branch moves to a new revert commit.
Verify both commits:
git log -2 --oneline
git show --stat HEAD
A revert can conflict when later work changed the same lines. Resolve it like a merge conflict, stage the result, then run git revert --continue. Use git revert --abort to return to the pre-revert state.
Revert is safer for shared history because collaborators can fetch a normal new commit. It does not require everyone to replace an existing commit ID.
Exercise: commit a line, commit another unrelated line, then revert the first commit. Confirm that the unrelated later change remains.
Lesson completed