Git/
Lesson

You have made a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.. Maybe it introduced a bug, maybe the requirements changed, or maybe you just realized it was a mistake. Now you need to undo it. But here is the catch: if that commit has been pushed to a shared repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory., or if other work has been built on top of it, simply deleting it would cause chaos.

Enter git revert. Instead of erasing history, it creates new history. It makes a new commit that does the exact opposite of the commit you want to undo. The original commit stays in the timeline, but its effects are neutralized.

How revert works

When you revert a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed., Git analyzes what that commit changed and creates a new commit that reverses those exact changes:

Before revert:
A --- B --- C --- D (HEAD)
        |
     (bad commit)

After revert:
A --- B --- C --- D --- E (HEAD)
        |                   |
     (bad commit)      (revert of B)

Commit B introduced a bug. Commit E undoes exactly what B did. The project is back to the state it was in before B, but the history shows both the mistake and the correction.

02

Reverting the last commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.

git revert HEAD

This opens your editor with a pre-filled commit message. Save and close to create the revert commit.

# Skip the editor and use the default message
git revert --no-edit HEAD
03

Reverting a specific commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.

The bad commit was not the last one, it was three commits ago:

git revert a1b2c3d
AI pitfall
When you ask AI "how do I undo a commit that was already pushed?", it sometimes suggests git reset --hard followed by git push --force. This rewrites public history and breaks every teammate's local repository. The correct answer is always git revert for pushed commits. Revert is safe because it adds new history rather than deleting existing history.
04

Reverting multiple commits

# Revert a range (exclusive start, inclusive end)
git revert abc1234..def5678

# Revert specific commits
git revert a1b2c3d e4f5g6h i7j8k9l
05

Handling revert conflicts

Reverting is not always automatic. If the code has changed significantly since the original commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed., Git might not know how to apply the reverse changes:

git revert abc1234
# CONFLICT (content): Merge conflict in src/auth.js
# error: could not revert abc1234

Resolving revert conflicts

  1. Open the conflicted file and look for conflict markers (<<<<<<<, =======, >>>>>>>)
  2. Edit the file to what it should be
  3. Remove the conflict markers
  4. Stage the resolved file: git add src/auth.js
  5. Continue the revert: git revert --continue

Aborting a revert

git revert --abort

This cancels the revert and returns you to the state before you started.

06

Revert vs reset: choosing the right tool

Aspectgit revertgit reset
MechanismCreates new commit that undoes changesMoves branch pointer back
HistoryPreserves all historyRewrites/deletes history
SafetySafe for shared branchesDangerous for shared branches
After pushWorks fineCauses problems (requires force push)
Commit countAdds one commitRemoves commits

When to use revert

  • The commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. has been pushed to a remote repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.
  • Other people might have pulled the commit
  • You are working on a shared branch (main, develop, etc.)
  • You want a record that the change was made and then undone

When to use reset

  • The commit is completely local (never pushed)
  • You are on a private branch that no one else uses
  • You want to erase the commit from history entirely
07

Real-world revert workflow

# Find the bad commit
git log --oneline -10
# a1b2c3d Add dark mode toggle
# e4f5g6h Fix navigation bug  <-- this one introduced a bug
# i7j8k9l Update dependencies

# Revert it
git revert e4f5g6h

# Push the revert
git push

Now the remote has both the mistake and the correction. Everyone who pulls gets the fix without any history conflicts.

08

Reverting a revert

Sometimes you revert something, then realize you actually needed it:

# The revert commit has its own hash
git log --oneline -3
# b2c3d4e Revert "Fix navigation bug"  <-- the revert
# a1b2c3d Add dark mode toggle
# e4f5g6h Fix navigation bug

# Revert the revert to bring the original changes back
git revert b2c3d4e
09

Quick reference

CommandWhat it does
git revert HEADRevert the last commit
git revert abc123Revert specific commit
git revert --no-edit HEADRevert without editing message
git revert abc123..def456Revert a range of commits
git revert --abortCancel an in-progress revert
git revert --continueContinue after resolving conflicts