BACK_TO_COURSE
The Git Rescue Kit — Undo Anything
Reset vs. Revert vs. Checkout — Pick the Right Undo
Three Commands, Three Very Different Blast Radii
| Command | What it does | Rewrites shared history? |
|---|---|---|
git revert <commit> |
Creates a new commit that undoes an old one | No — safe on main |
git reset <commit> |
Moves your branch pointer backward (optionally touching staged/working files too) | Yes — local/unshared only |
git checkout -- <file> / git restore <file> |
Discards uncommitted changes to a single file | N/A — working dir only |
The Three Flavors of Reset
git reset --soft HEAD~1 # undo the commit, keep changes staged
git reset --mixed HEAD~1 # undo the commit AND unstage changes (default)
git reset --hard HEAD~1 # undo the commit AND discard the changes entirely
--hard is the only genuinely destructive one of the three — it throws away uncommitted work with no confirmation prompt. On a commit that's already been pushed and pulled by someone else, use revert instead of reset, because revert adds new history instead of erasing shared history out from under other people.
WARNING
If a bad commit already reached main and other people have it, git reset --hard on main doesn't undo it for them — it just makes your local history disagree with everyone else's, which is a much worse Tuesday than the original bug. git revert is the correct fix for anything already shared.
QUIZ
A bad commit was pushed to `main` and three teammates already pulled it. What's the correct way to undo it?
Sign in to track lesson progress and earn a certificate.SIGN IN TO SAVE PROGRESS