git revert
Undo a commit by creating a new commit
The 'git revert' command undoes an earlier commit by adding a new commit that applies the opposite change. History is not rewritten — the original commit stays — so it is the safe way to undo commits that were already pushed and shared. It can conflict when later commits touched the same lines; resolve, then git revert --continue.
Synopsis
git revert [--[no-]edit] [-n] [-m <parent-number>] <commit>...
git revert (--continue | --skip | --abort | --quit)Options
Choosing the Right Option
revert vs reset
Two ways to undo commits
| Command | History | Safe after push? |
|---|---|---|
git revert <commit> | Adds an undo commit | Yes |
git reset --hard <commit> | Removes commits from the branch | No — needs a force push |
Examples
Undo the last commit
git revert HEADCreates a commit that undoes HEAD.
Undo an older commit
git revert a1b2c3dUndoes that commit, leaving later ones.
Undo a merge
git revert -m 1 <merge-commit>Undoes the changes a merge brought in.
Workflow Examples
git log --onelineFind the commit
git revert a1b2c3dCreate the undo commit
git pushShare it — no force needed
Troubleshooting
error: commit is a merge but no -m option was given
Cause: A merge has two parents
Use git revert -m 1 <commit>
CONFLICT during revert
Cause: Later commits changed the same lines
Fix the files, git add them, then git revert --continue (or --abort)
Tips & Pitfalls
Best Practices
- ✓Revert shared commits; reset only local ones.
- ✓Explain why in the revert message.
- ✓Revert newest first when undoing several commits.
Common Mistakes
- ✗Using git reset on pushed commits instead of git revert, and forcing everyone to fix their history.
- ✗Reverting a merge without -m.
- ✗Re-merging a reverted branch and wondering why its changes do not come back — revert the revert first.
FAQ
No. It adds a new commit; the original stays in history.
Yes — it re-applies the original change.
git revert --abort.