Skip to content

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.

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.

Terminal window
git revert a3f8c12

Git 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:

Terminal window
git revert a3f8c12 --no-edit

Use git log --oneline to find the hash:

d4f2a01 (HEAD -> main) Add group size limit to Summit Challenge
c2a1d08 Add viewpoint note to Pine Ridge
b7e9d03 Add difficulty notes
a3f8c12 Add initial tour notes

To revert the most recent commit:

Terminal window
git revert HEAD

To revert a commit that is two back:

Terminal window
git revert HEAD~2

HEAD~2 means “two commits before HEAD.”

To revert a range of commits (most recent first):

Terminal window
git revert HEAD~3..HEAD

This 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:

Terminal window
git revert --no-commit HEAD~3..HEAD
git 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.

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:

Terminal window
git revert --abort
git revertgit reset
Rewrites history?No — adds a new commitYes — moves the branch pointer
Safe for shared repos?YesNo (for pushed commits)
Effect on commit historyPreserves the original commitRemoves commits from history
Creates a commit?YesNo (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).

  1. In your git-practice folder, make a commit you want to undo:
Terminal window
echo "WRONG: this should not be here" >> pine-ridge.txt
git add pine-ridge.txt
git commit -m "Add incorrect note to Pine Ridge"
  1. Revert it:
Terminal window
git revert HEAD --no-edit
  1. Run git log --oneline. You should see both the original commit and the revert commit in history.

  2. Open pine-ridge.txt and confirm the incorrect line is gone, and that everything else in the file is intact.

  3. Run git diff HEAD~1 HEAD to 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 HEAD reverts the most recent commit. HEAD~2 is two commits back.
  • --no-edit accepts 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.