git revert
Undoing commits safely with git revert — creating an inverse commit without rewriting history.
What you'll learn
- How git revert creates a new commit that undoes a previous one
- Why revert is safer than reset for shared branches
- Reverting a merge commit
- Handling revert conflicts
Concept
What git revert Does
git revert creates a new commit that undoes the changes from a specified commit. Instead of rewriting history, it adds an inverse commit on top.
git revert abc1234
This is the safe way to undo a commit, especially on shared branches. Because it creates a new commit (rather than removing the old one), the history is preserved and other developers who have already pulled are not affected.
revert vs reset
This is a critical distinction:
git revert— creates a new commit that undoes the changes. History is preserved. Safe for shared branches.git reset— moves the branch pointer back, removing commits from history. Dangerous for shared branches.
Original history: A---B---C---D (main)
After git revert C: A---B---C---D---C' (main)
(C' is a new commit that undoes C)
After git reset C: A---B---C (main)
(D is gone from history)
Use revert when others have already seen the commits. Use reset only on local, unpushed commits.
Reverting the Last Commit
git revert HEAD
This opens your editor with a default message like Revert "Add broken feature". You can edit it and save, or use -m to skip the editor:
git revert HEAD --no-edit
Reverting a Range of Commits
# Revert commits from HEAD~3 to HEAD~1 (inclusive)
git revert HEAD~3..HEAD~1
Each commit in the range gets its own revert commit, applied in order.
Reverting a Merge Commit
Reverting a merge commit is trickier because Git needs to know which parent was the mainline:
git revert -m 1 merge-commit-hash
The -m 1 flag specifies parent 1 (the branch you merged into) as the mainline. This undoes the changes introduced by the merge.
Handling Conflicts
If the code has changed since the commit you are reverting, Git may report a conflict:
git revert abc1234
# CONFLICT (content): Merge conflict in login.js
# Resolve the conflict in your editor, then:
git add login.js
git revert --continue
# Or cancel the revert:
git revert --abort
When to Use revert
- You pushed a commit that broke the build →
git revert - You want to undo a commit on a shared branch →
git revert - You accidentally merged a feature that was not ready →
git revert -m 1 - You need an audit trail of what was undone →
git revert(the revert commit records it)
Example
# View recent history to find the commit to revert
git log --oneline -5
# d4c3b2a feat: add payment processing
# a1b2c3d fix: correct login redirect
# f6e5d4c docs: update README
# Revert the payment feature (creates a new undo commit)
git revert d4c3b2a
# Git opens an editor with: "Revert "feat: add payment processing""
# Save and close to complete the revert
# Revert without opening the editor
git revert HEAD --no-edit
# View the result — the revert commit is on top
git log --oneline -3
# xxxxxxx Revert "feat: add payment processing"
# d4c3b2a feat: add payment processing
# a1b2c3d fix: correct login redirect
# Revert a range of commits
git revert HEAD~3..HEAD~1
# Revert a merge commit (specify mainline parent)
git revert -m 1 abc1234
# If a revert causes a conflict, resolve and continue
git revert HEAD
# ... resolve conflict in editor ...
git add conflicted-file.js
git revert --continue
# Cancel a revert that went wrong
git revert --abort
Shows reverting a specific commit, reverting HEAD, reverting without editing the message, reverting a range, reverting a merge commit with -m 1, and handling conflicts. The key insight is that revert adds a new commit rather than removing history.
Try it
- Code Diff
See what a revert will undo before running it.
- CI/CD YAML Generator
Revert commits trigger CI to verify the fix works.
Common mistakes
The mistake
Using git reset --hard on a shared branch instead of git revert
The fix
git reset rewrites history, which breaks things for anyone who already pulled the commits. On shared branches, always use git revert to create a new commit that undoes the changes. Reserve git reset for local, unpushed commits only.
The mistake
Forgetting that revert creates a NEW commit, not removing the old one
The fix
git revert does not delete the original commit — it creates a new commit that applies the inverse changes. The original commit stays in history. This is intentional: it preserves an audit trail and is safe for shared branches. If you need to remove commits entirely, use git reset (locally only).
Related Resources
Related Cheatsheets
- Git Cheatsheet
revert, revert --no-edit, and revert -m commands.
Practice git revert
3 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.