Undoing anything
revert, reset, restore, and reflog — git's safety net, demystified.
- Discard or unstage changes with git restore
- Choose between revert (safe, public) and reset (rewrites history)
- Recover "lost" commits with the reflog
Git's superpower is that almost nothing is truly lost. The catch is that the undo commands have similar names and very different effects. Learn the four that matter and you can confidently dig yourself out of any hole — which is exactly the self-reliance this track is about.
restore — undo uncommitted work
git restore deals with changes you haven't committed yet:
git restore file.js # discard working-tree edits to file.js
git restore --staged file.js # unstage it (keep the edits, just un-add)The first one throws away changes — use it when an edit went wrong. The second
just reverses a git add.
revert — safely undo a commit
git revert undoes a commit by creating a new commit that reverses it. The
original stays in history:
git revert a1b2c3dBecause it adds rather than rewrites, revert is safe on shared history —
the public record stays intact. This is your default for undoing something
others may already have.
reset — move the branch pointer
git reset moves your branch back to an earlier commit, rewriting history
from that point:
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --mixed HEAD~1 # undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1 # undo last commit AND discard its changes--soft/--mixed are great for "I committed too early, let me redo it."
--hard is the one to respect — it deletes work. And because reset rewrites
history, follow the golden rule: don't reset commits you've already pushed and
shared.
reflog — the ultimate safety net
Even after a reset --hard, the old commits aren't gone immediately. git reflog
records every position your branch has been at:
git reflog # find the hash you were on before the mistake
git reset --hard a1b2c3d # jump back to itWhen the agent's away: revert, reset, and reflog mean a bad commit or a botched merge is never a dead end. Knowing them turns "I broke everything" into a 30-second fix you can do yourself.
Where to go next
Next: stash and cherry-pick — setting work aside temporarily and plucking individual commits between branches.