Made a mistake in a commit? Need to undo changes that have already been pushed? git revert is your safest option. Unlike destructive commands that erase history, git revert creates a new commit that undoes the changes - keeping your history intact and your team happy.
What You'll Learn: In this comprehensive guide, you'll master:
- How
git revertworks and why it's safe - The difference between
git revertandgit reset - Reverting commits step-by-step with real examples
- Understanding the revert commit message format
- How revert affects local and remote repositories
- When to use revert vs other undo methods
Why Revert Instead of Delete?
When you need to undo a commit, you have two main approaches:
Option A: DESTRUCTIVE (git reset)
================================
Before: A---B---C---D (D is the bad commit)
After: A---B---C (D is erased from history)
Option B: SAFE (git revert)
===========================
Before: A---B---C---D (D is the bad commit)
After: A---B---C---D---E (E undoes D's changes)
└── "Revert D"
Key Insight: git revert doesn't remove history - it adds new history that undoes the old changes. This is crucial for shared repositories!
When to Use Git Revert
| Situation | Use Revert? | Why |
|---|---|---|
| Commit already pushed | Yes | Safe for shared history |
| Working with a team | Yes | Doesn't disrupt others' work |
| Need audit trail | Yes | Shows what was undone and when |
| Private local commits | Optional | Reset might be simpler |
| Multiple commits to undo | Yes | Can revert each individually |
Basic Git Revert Workflow
Let's walk through a real example. Here's our starting repository:
git log
Output:
commit 795429a1faa5bb77f9e3c052d32db92e62b008a7 (HEAD -> master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Jan 26 14:37:37 2026 +0500
added fourth.txt
commit 87de0f7ad3b2e2f1e68e2f17fe1c2a559a87ce24
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Sun Oct 19 02:45:34 2025 +0500
added third.txt
commit 8c331caa10309fcb76a8cbd38c4f2e8cbff80b64
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Tue Sep 30 23:47:49 2025 +0500
Update second.txt on github
The Current State
Commit History:
HEAD
|
8c331ca ----> 87de0f7 ----> 795429a ----> master
| | |
"Update "added "added
second.txt" third.txt" fourth.txt"
Let's say we need to undo the "added fourth.txt" commit (795429a).
Step 1: Identify the Commit to Revert
You can use the commit hash (full or short):
# Full hash
git revert 795429a1faa5bb77f9e3c052d32db92e62b008a7
# Short hash (easier)
git revert 795429a
Step 2: Execute the Revert
git revert 795429a
Git opens your default editor with a pre-filled commit message:
Revert "added fourth.txt"
This reverts commit 795429a1faa5bb77f9e3c052d32db92e62b008a7.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
Save and close the editor to complete the revert.
Output:
[master ce0024b] Revert "added fourth.txt"
1 file changed, 1 deletion(-)
delete mode 100644 Fourth.txt
Step 3: Verify the Revert
Check your files:
ls
Before revert:
first.txt Fourth.txt second.txt third.txt
After revert:
first.txt second.txt third.txt
The Fourth.txt file is gone!
Understanding the Revert Commit
Let's examine what git revert created:
git log
Output:
commit ce0024b5fc0365ab9830c94eee7be465b141a617 (HEAD -> master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Jan 26 14:42:05 2026 +0500
Revert "added fourth.txt"
This reverts commit 795429a1faa5bb77f9e3c052d32db92e62b008a7.
commit 795429a1faa5bb77f9e3c052d32db92e62b008a7
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Jan 26 14:37:37 2026 +0500
added fourth.txt
commit 87de0f7ad3b2e2f1e68e2f17fe1c2a559a87ce24
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Sun Oct 19 02:45:34 2025 +0500
added third.txt
What This Shows
Before Revert:
87de0f7 ----> 795429a (HEAD)
| |
"added "added
third.txt" fourth.txt"
After Revert:
87de0f7 ----> 795429a ----> ce0024b (HEAD)
| | |
"added "added "Revert
third.txt" fourth.txt" added fourth.txt"
The original commit (795429a) is PRESERVED!
A new commit (ce0024b) UNDOES its changes.
Notice: The original commit 795429a is still in the history! We can see exactly what was done and when it was undone. This is the power of revert.
Syncing the Revert with Remote
The revert only affected your local repository. To apply it to GitHub:
git push hello master
Output:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 274 bytes | 274.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/owais-io/secondproject.git
479cd2a..ce0024b master -> master
After Push - Both Repos Match
git log
Output:
commit ce0024b5fc0365ab9830c94eee7be465b141a617 (HEAD -> master, hello/master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Jan 26 14:42:05 2026 +0500
Revert "added fourth.txt"
This reverts commit 795429a1faa5bb77f9e3c052d32db92e62b008a7.
Notice hello/master now points to the same commit as your local master.
Revert vs Reset: A Visual Comparison
SCENARIO: Commits A-B-C-D exist, we want to undo D
============================================================
GIT REVERT (Safe - Adds History)
============================================================
Before: A ----> B ----> C ----> D (HEAD)
Command: git revert D
After: A ----> B ----> C ----> D ----> E (HEAD)
|
"Revert D"
(undoes D's changes)
Result:
- D still exists in history
- E contains the opposite of D's changes
- Safe to push (no force required)
- Team members won't have conflicts
============================================================
GIT RESET --hard (Dangerous - Removes History)
============================================================
Before: A ----> B ----> C ----> D (HEAD)
Command: git reset --hard C
After: A ----> B ----> C (HEAD)
D is GONE!
Result:
- D is erased from history
- Requires force push (git push -f)
- Team members will have conflicts
- Audit trail is lost
Reverting Multiple Commits
Method 1: Revert One at a Time (Recommended)
# Revert the most recent commit
git revert HEAD
# Revert a specific commit
git revert abc123
# Revert another commit
git revert def456
Each revert creates its own commit, maintaining a clear history.
Method 2: Revert a Range
# Revert commits from older to newer (exclusive..inclusive)
git revert older_commit..newer_commit
Method 3: Revert Without Auto-Commit
If you want to revert multiple commits into a single commit:
# Stage the reverts without committing
git revert --no-commit abc123
git revert --no-commit def456
# Now commit all reverts together
git commit -m "Revert multiple commits: abc123 and def456"
Common Revert Options
| Option | Description |
|---|---|
git revert <commit> | Revert and auto-commit |
git revert -n <commit> | Revert but don't commit (stage only) |
git revert --no-edit <commit> | Revert with default message (no editor) |
git revert -m 1 <merge> | Revert a merge commit (specify parent) |
Handling Revert Conflicts
Sometimes reverting causes conflicts (if later commits modified the same files):
git revert abc123
Output:
error: could not revert abc123... commit message
hint: after resolving the conflicts, mark them with
hint: "git add <file>" and run "git revert --continue"
Resolve the Conflict
- Open the conflicted file and resolve manually
- Stage the resolution:
git add <file> - Continue the revert:
git revert --continue
Or abort the revert entirely:
git revert --abort
Reverting a Revert
Yes, you can undo an undo! If you reverted something by mistake:
# Find the revert commit
git log --oneline
# Output:
# ce0024b Revert "added fourth.txt"
# 795429a added fourth.txt
# Revert the revert!
git revert ce0024b
This brings back the original changes.
Best Practices
1. Always Check Before Reverting
# See what the commit changed
git show <commit-hash>
# See the diff
git diff <commit-hash>^ <commit-hash>
2. Use Descriptive Revert Messages
The default message is good, but you can add context:
Revert "added fourth.txt"
This reverts commit 795429a1faa5bb77f9e3c052d32db92e62b008a7.
Reason: File was added to wrong branch. Should be in feature-x.
3. Communicate with Your Team
Before reverting shared commits, let your team know:
- What you're reverting
- Why you're reverting it
- When you'll push the revert
Quick Reference
# Basic revert
git revert <commit-hash>
# Revert without opening editor
git revert --no-edit <commit-hash>
# Revert without committing (stage only)
git revert -n <commit-hash>
# Revert a merge commit
git revert -m 1 <merge-commit-hash>
# Abort a revert in progress
git revert --abort
# Continue after resolving conflicts
git revert --continue
Summary
git revert is the safest way to undo commits because it:
- Preserves history: The original commit stays visible
- Creates audit trail: Shows what was undone and when
- Safe for teams: No force push required
- Reversible: You can revert a revert
Use git revert when working with shared branches or when you need to maintain a complete history of all changes - including mistakes and their corrections!

