Git Revert
Last updated
git revert undoes a commit by creating a new commit that applies the inverse of its changes. Because it adds history rather than rewriting it, revert is the safe way to undo a commit that's already been pushed - unlike git reset, it won't force everyone else to reconcile a rewritten branch.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git revert HEAD | Revert the most recent commit |
git revert <hash> | Revert a specific commit by hash |
git revert HEAD~2..HEAD | Revert a range of commits |
git revert -n <hash> | Revert but don't commit yet (stage only) |
git revert -m 1 <merge-hash> | Revert a merge commit, keeping parent 1 |
During a revert
If the revert hits a conflict, resolve it and continue.
| Command | What it does |
|---|---|
git revert --continue | Resume after resolving conflicts |
git revert --abort | Cancel the revert in progress |
git revert --skip | Skip the current commit and continue |
revert vs reset
| Behavior | git revert | git reset |
|---|---|---|
| Rewrites history | No | Yes |
| Safe on pushed commits | Yes | No |
| Creates a new commit | Yes | No |
| Undoes a commit in the middle | Yes | Awkward |
Git revert FAQ
What's the difference between git revert and git reset?
git revert adds a new commit that undoes the changes of an earlier one, leaving history intact - safe for commits you've already pushed. git reset moves your branch pointer backward and rewrites history, which is fine for local commits but dangerous on shared branches. Use revert to undo public commits, reset to undo local ones.How do I revert a commit that was already pushed?
Run
git revert <hash> (or git revert HEAD for the latest), then push the new revert commit. Because revert doesn't rewrite history, this is the correct way to undo a shared commit - no force-push and no disruption for teammates who already pulled it.How do I revert a merge commit?
A merge commit has two parents, so Git needs to know which line of history to keep. Use
git revert -m 1 <merge-hash>, where -m 1 tells Git to keep the first parent (usually the branch you merged into). Reverting a merge undoes the merged changes but keeps the merge in history.Can I revert multiple commits at once?
Yes. Pass a range like
git revert HEAD~2..HEAD to revert the last two commits, or list several hashes. Git creates one revert commit per reverted commit by default; add -n to stage all the reversals without committing, then make a single commit yourself.Can I practice this online?
Yes. Open the terminal playground to run
git revert in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers undoing changes step by step.