Git Revert: Undo Pushed Commits Safely (Complete Guide for 2026)
git revert is the safest undo command for shared branches. Instead of rewriting history, it creates a new commit that inverses an older commit.
If your commit is already on main, in a merged PR, or on any branch teammates might have pulled, revert is usually the right move.
Table of contents
1. Copy/paste revert recipes
Undo the latest commit safely (shared branch)
git revert HEAD
git push origin main
Undo a specific old commit
git revert <commit-hash>
Revert several commits, then make one combined revert commit
git revert --no-commit OLDEST^..NEWEST
git commit -m "Revert problematic auth changes"
Revert a merge commit
git revert -m 1 <merge-hash>
Undo a bad revert (reapply original changes)
git revert <revert-commit-hash>
git show --name-only <hash> before reverting to confirm you are targeting the right commit.
2. Revert vs reset vs cherry-pick
| Goal | Command | History rewritten? |
|---|---|---|
| Undo a pushed commit safely | git revert <hash> |
No |
| Delete local commits and move branch pointer | git reset --hard HEAD~N |
Yes (if force-pushed) |
| Copy a commit onto another branch | git cherry-pick <hash> |
No |
git revert. Keep git reset for local-only history cleanup.
3. Revert one commit
Case A: undo the most recent commit on main
git switch main
git pull --ff-only
git revert HEAD
git push origin main
Case B: undo an older commit by hash
git log --oneline -n 30
# copy hash
git revert a1b2c3d
If this introduces conflicts, Git pauses and asks you to resolve them. Resolve files, then continue:
git add -A
git revert --continue
To cancel the in-progress revert:
git revert --abort
4. Revert multiple commits
You have two common workflows.
Workflow A: one revert commit per original commit
git revert commit1
git revert commit2
git revert commit3
Use this when you want a clear audit trail for each undone commit.
Workflow B: one combined revert commit
git revert --no-commit OLDEST^..NEWEST
# inspect staged changes
git status
git diff --cached
git commit -m "Revert checkout refactor and follow-up fixes"
Use this when several commits are part of one rollback event.
A^..B includes commit A through B. Without ^, the oldest commit is excluded.
5. Revert a merge commit
Merge commits have multiple parents, so Git needs a mainline parent:
git revert -m 1 <merge-hash>
-m 1: keep parent 1 (commonly the target branch such asmain)-m 2: keep parent 2
Verify parents before reverting:
git show --no-patch --pretty=raw <merge-hash>
Need the full parent-selection walkthrough? Use Git Revert a Merge Commit.
6. Conflict handling and safer review workflow
For production rollbacks, it is often safer to apply reverts without auto-committing, inspect the result, then commit.
git revert --no-commit <hash-or-range>
# inspect before creating the revert commit
git status
git diff --cached
git commit -m "Revert payment retry changes"
If you hit conflicts:
# resolve files in your editor
git add -A
git revert --continue
# if needed
git revert --abort
7. Revert a revert (bring code back)
If the original issue is fixed and you want to reapply reverted code, revert the revert commit:
git log --oneline -n 20
# find the revert commit
git revert <revert-commit-hash>
This is cleaner than force-pushing old history and keeps a transparent audit trail.
8. Team-safe revert checklist
- Identify exact commit hash(es) with
git log --oneline. - Prefer
git reverton any shared branch. - Use
--no-commitwhen reverting ranges or high-risk changes. - Run tests before and after pushing the revert.
- Document why the revert happened in the commit message.
FAQ
What is the safest way to undo a pushed commit in Git?
Use git revert <commit-hash> (or git revert HEAD for the latest commit). It creates a new undo commit without rewriting history.
What is the difference between git revert and git reset?
revert preserves history by adding a new commit. reset moves branch pointers and can rewrite history when force-pushed.
How do I revert multiple commits at once?
Use a range with --no-commit, then create one commit: git revert --no-commit OLDEST^..NEWEST.
How do I revert a merge commit?
Use git revert -m 1 <merge-hash> after confirming parent order with git show --pretty=raw.
Can I bring changes back after a revert?
Yes. Revert the revert commit with git revert <revert-hash>.
Related Resources
-m parent selection and re-merge workflows.
Undo Last Commit
Fast commands for the most common local and pushed cases.