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.
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 HEADThis 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 HEADReverting 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 a1b2c3dgit 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.Reverting multiple commits
# Revert a range (exclusive start, inclusive end)
git revert abc1234..def5678
# Revert specific commits
git revert a1b2c3d e4f5g6h i7j8k9lHandling 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 abc1234Resolving revert conflicts
- Open the conflicted file and look for conflict markers (
<<<<<<<,=======,>>>>>>>) - Edit the file to what it should be
- Remove the conflict markers
- Stage the resolved file:
git add src/auth.js - Continue the revert:
git revert --continue
Aborting a revert
git revert --abortThis cancels the revert and returns you to the state before you started.
Revert vs reset: choosing the right tool
| Aspect | git revert | git reset |
|---|---|---|
| Mechanism | Creates new commit that undoes changes | Moves branch pointer back |
| History | Preserves all history | Rewrites/deletes history |
| Safety | Safe for shared branches | Dangerous for shared branches |
| After push | Works fine | Causes problems (requires force push) |
| Commit count | Adds one commit | Removes 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
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 pushNow the remote has both the mistake and the correction. Everyone who pulls gets the fix without any history conflicts.
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 b2c3d4eQuick reference
| Command | What it does |
|---|---|
git revert HEAD | Revert the last commit |
git revert abc123 | Revert specific commit |
git revert --no-edit HEAD | Revert without editing message |
git revert abc123..def456 | Revert a range of commits |
git revert --abort | Cancel an in-progress revert |
git revert --continue | Continue after resolving conflicts |