git revert — Undoing a Commit Safely
You pushed a commit that introduced a bug, or merged a feature that needs to be pulled back, or simply need to undo a change that has already been shared with others. git revert creates a new commit that applies the inverse of a previous commit — effectively undoing its changes while leaving the original commit in history.
How git revert works
Section titled “How git revert works”Instead of removing a commit from history (which would require rewriting history and break shared repos), git revert adds a new commit that reverses whatever the target commit did.
If commit a3f8c12 added three lines to pine-ridge.txt, a revert of that commit removes those same three lines. The original commit still exists in history. The revert commit also exists. Anyone who pulls the repository gets both — and the end result is that the three lines are gone.
Basic usage
Section titled “Basic usage”git revert a3f8c12Git computes the inverse of that commit’s changes, applies them to the working tree, stages them, and opens your editor with a pre-filled commit message:
Revert "Add viewpoint note to Pine Ridge description"
This reverts commit a3f8c12d4e9f...Save and close the editor to complete the revert commit. Or use --no-edit to accept the message without opening the editor:
git revert a3f8c12 --no-editFinding the commit to revert
Section titled “Finding the commit to revert”Use git log --oneline to find the hash:
d4f2a01 (HEAD -> main) Add group size limit to Summit Challengec2a1d08 Add viewpoint note to Pine Ridgeb7e9d03 Add difficulty notesa3f8c12 Add initial tour notesTo revert the most recent commit:
git revert HEADTo revert a commit that is two back:
git revert HEAD~2HEAD~2 means “two commits before HEAD.”
Reverting multiple commits
Section titled “Reverting multiple commits”To revert a range of commits (most recent first):
git revert HEAD~3..HEADThis reverts three commits, creating a separate revert commit for each. Use --no-edit to avoid opening the editor for each one.
Or create a single revert commit for the whole range:
git revert --no-commit HEAD~3..HEADgit commit -m "Revert last three commits"--no-commit (or -n) applies the reversals to the working tree and staging area without committing, letting you combine them into one commit.
When revert produces a conflict
Section titled “When revert produces a conflict”If the changes in the commit you are reverting overlap with later changes in the same file, Git will report a conflict — just like a merge conflict. Resolve the conflict markers, stage the file, and run git revert --continue.
To abandon a revert in progress:
git revert --abortgit revert vs. git reset
Section titled “git revert vs. git reset”git revert | git reset | |
|---|---|---|
| Rewrites history? | No — adds a new commit | Yes — moves the branch pointer |
| Safe for shared repos? | Yes | No (for pushed commits) |
| Effect on commit history | Preserves the original commit | Removes commits from history |
| Creates a commit? | Yes | No (reset just moves the pointer) |
Use git revert for commits that have been pushed to a shared repository. Use git reset for local-only commits (covered in the next lesson).
Exercise
Section titled “Exercise”- In your
git-practicefolder, make a commit you want to undo:
echo "WRONG: this should not be here" >> pine-ridge.txtgit add pine-ridge.txtgit commit -m "Add incorrect note to Pine Ridge"- Revert it:
git revert HEAD --no-edit-
Run
git log --oneline. You should see both the original commit and the revert commit in history. -
Open
pine-ridge.txtand confirm the incorrect line is gone, and that everything else in the file is intact. -
Run
git diff HEAD~1 HEADto see what the revert commit changed — it should be the inverse of what the original commit added.
git revert <hash>creates a new commit that is the inverse of the target commit — undoing its changes without removing it from history.- Safe to use on pushed commits. Does not rewrite history.
git revert HEADreverts the most recent commit.HEAD~2is two commits back.--no-editaccepts the pre-filled revert message without opening the editor.--no-commit(or-n) applies the reversal to the staging area without committing, useful for combining multiple reverts.- Revert conflicts are resolved the same way as merge conflicts.
Next: git reset — how to rewind the branch pointer and the three modes that determine what happens to staged and working-tree changes.