Menu
Coddy logo textTech

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.

git revert leaves the bad commit in place and adds a new commit that applies its inverse - history is never rewritten.

Syntax

CommandWhat it does
git revert HEADRevert the most recent commit
git revert <hash>Revert a specific commit by hash
git revert HEAD~2..HEADRevert 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.

CommandWhat it does
git revert --continueResume after resolving conflicts
git revert --abortCancel the revert in progress
git revert --skipSkip the current commit and continue

revert vs reset

Behaviorgit revertgit reset
Rewrites historyNoYes
Safe on pushed commitsYesNo
Creates a new commitYesNo
Undoes a commit in the middleYesAwkward

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.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED