Last Updated: May 1, 2025
Revert Commands
git revert HEADRevert the most recent commit
git revert commitRevert a specific commit by its SHA
git revert HEAD~3..HEADRevert a range of commits (oldest first)
git revert --no-commit commitRevert without auto-committing (review changes first)
git revert --no-editSkip editing the auto-generated commit message
git revert --signoffAdd Signed-off-by line to the revert commit
git revert --continueContinue revert after resolving conflicts
git revert --abortAbort a revert that has conflicts
Reverting Merge Commits
git revert -m 1 merge-commitRevert a merge commit (specify mainline parent)
git show merge-commitFind parent numbers: Parent 1 = current branch, Parent 2 = merged
git revert -m 1 --no-commit mergeReview merge revert before committing
git revert merge-commitPlain revert of a merge WILL FAIL — must use -m flag
git log --mergesList all merge commits to find the one to revert
Revert vs Reset
| Aspect | git revert | git reset |
|---|---|---|
| Safety | Safe — adds new commit | Dangerous — removes history |
| Shared Branches | Recommended | Avoid at all costs |
| Private Branches | Works but verbose | Faster, cleaner |
| Undo | git revert the revert commit | git reflog recovery |
| History | Full audit trail | History disappears |
| Conflicts | May have conflicts | No conflicts (just moves pointer) |
| Team Impact | None — everyone pulls safely | Force push required — breaks others |
Real-World Revert Patterns
| Item | Description |
|---|---|
Revert the Revert | If you revert by mistake, revert the revert commit to restore |
Partial Revert | git revert --no-commit → manually edit → commit only what you want |
Sequential Reverts | Revert multiple commits in reverse chronological order |
Hotfix Rollback | git revert the bad deploy commit on main — no force push needed |
CI/CD Safety | Revert commits that broke CI, fix forward, then re-apply |
Feature Flag + Revert | Ship behind flag, revert config change instead of code |
Merge Revert Gotcha | Re-reverting a merge requires the SAME -m parent number |
Pro Tip: Always use `git revert` on shared branches instead of `git reset` — reset rewrites history and will break your teammates' repos.