Reset & Revert
You might say
I just committed the wrong thing. How do I undo it?
Two ways to undo committed work: reset moves the branch pointer, revert adds an opposite commitWhen a faulty commit exists only locally, reset moves the branch pointer backward to cleanly discard or unstage it; if that commit has already been pushed to a shared remote, use revert to append an offsetting commit instead. revert preserves collaborative history intact, whereas reset alters where the current branch pointer points.
ResetRevert
When to use it
- Undo a local commit by resetting back one$ git reset --hard HEAD~1HEAD is now at 3f9c2ab Adjust navigation spacing
- Keep changes but redo the commit with --soft$ git reset --soft HEAD~1Changes stay staged; restage and commit after tidying up
- Undo a pushed commit with revert$ git revert a1b2c3d[main 9e8f7a6] Revert "Add debug code by mistake"
- Check before --hard discards uncommitted workgit status shows uncommitted changesreset --hard discards these changes too
When NOT to use it
- Reset a pushed commit and force push over teammates' historyForce pushing after resetting a pushed commit splits teammates' copiesDo not rewrite shared history; use revert
- Run reset --hard blindly and lose uncommitted workTwo hours of uncommitted work disappear with reset --hardRun git status first to confirm
- Revert someone else's commit without telling themReverted a teammate's commit without telling themThey keep building on reverted work, creating needless conflicts
- Skip verification after undoingreset ✓→The page errors
Anatomy
HEAD
Back to here
Reset removes it from the branch line; revert keeps it and adds an opposite commit.
Variants
--soft
git reset --soft HEAD~1
Undo the commit but keep the changes to restage them
--hard
git reset --hard HEAD~1
Return to the previous commit and discard the changes
revert
git revert a1b2c3d
Undo a pushed commit without rewriting shared history
Typical use cases
git reset in a terminal
$ git reset --hard HEAD~1
HEAD is now at 3f9c2ab Adjust navigation spacing
The branch pointer moved back; the wrong commit leaves this line
History back at the previous commit
main
After reset, HEAD points at Adjust navigation spacing; the wrong commit is gone from history
git revert in a terminal
$ git revert a1b2c3d
[main 9e8f7a6] Revert "Add debug code by mistake"
A new commit cancels the changes in a1b2c3d
A Revert commit in git log
main
Revert keeps history: the red commit stays, the green commit cancels its changes
Further reading