Reverting Commits
git revert undoes a commit by creating a new commit that applies the exact opposite change. Nothing is removed from history, so it's the safe way to undo something that has already been pushed and shared with others.
Undoing a single commit
The spring sale banner went live by mistake — and it's already on the shared main branch:
git log --oneline
1fe71c1 (HEAD -> main) Change text colour to navy 0b1407f Add spring sale banner f41375a Create home page
Revert it by naming the commit. Git opens your editor with a ready-made message; --no-edit accepts it as is:
git revert --no-edit HEAD~1
[main 8c77cce] Revert "Add spring sale banner" Date: Mon Mar 2 09:42:00 2026 +0000 1 file changed, 1 deletion(-)
git log --oneline cat index.html
8c77cce (HEAD -> main) Revert "Add spring sale banner" 1fe71c1 Change text colour to navy 0b1407f Add spring sale banner f41375a Create home page Welcome to the shop
The banner is gone, the later colour change is untouched, and the history shows exactly what happened and when — useful for anyone wondering where the banner went. The new commit's message includes the hash of the commit it reverted:
git log -1 --format=%B
Revert "Add spring sale banner" This reverts commit 0b1407fef2f00714dd69bc1507126985fd36cf5c.
Revert vs. reset
git reset | git revert | |
|---|---|---|
| How it undoes | Moves the branch back; later commits leave the branch | Adds a new commit with the opposite change |
| History | Rewritten | Preserved |
| Undo a commit in the middle | No — it removes everything after the target too | Yes — any single commit |
| Safe after pushing | No | Yes |
Reverting several commits
Give several commits, or a range. Each is reverted with its own new commit, newest first. Add --no-commit (-n) to apply all the reversals to your working tree and then commit them once yourself:
git revert --no-edit a1b2c3d e4f5a6b
git revert --no-commit HEAD~3..HEAD
git commit -m "Revert the checkout redesign"
Conflicts
If later commits changed the same lines as the commit you're reverting, Git can't work out the opposite change on its own and stops with a conflict. You resolve it just like a merge conflict (lesson 25), then run git revert --continue — or git revert --abort to give up and return to where you were.
Reverting a merge
A merge commit has two parents, so Git needs to know which side to keep. -m 1 means "keep the first parent" — the branch you merged into, usually main — and undo everything the merge brought in:
git revert -m 1 9f8e7d6
One subtlety: after reverting a merge, Git considers those commits already merged. If the branch is fixed and you want to merge it again later, you first have to revert the revert.
Reverting a revert
Changed your mind? A revert is an ordinary commit, so it can itself be reverted, which brings the original change back:
git revert --no-edit HEAD cat index.html
[main 905db89] Revert "Revert "Add spring sale banner"" Date: Mon Mar 2 10:03:00 2026 +0000 1 file changed, 1 insertion(+) Welcome to the shop Spring sale: 50% off everything!
Try it yourself
Make three commits that each change a different file. Revert the middle one, check that only its file changed back, then revert the revert. Look at the full history with git log --oneline — five commits, none removed.