Undoing Mistakes Safely: restore, reset, revert, reflog
The most reassuring news in the whole course: Git almost never truly loses your work. Nearly every mistake is reversible, if you know which tool to reach for. In this lesson we'll build a small, clear recovery toolbox of four tools, each paired with a 'when': git restore to throw away a not-yet-save
restore is 'undo what I'm editing now'. reset is 'take me back in time' (and --hard also deletes, so be careful). revert is 'add a page that cancels an old page' — team-safe. reflog is a security camera that remembers where HEAD moved, even when it feels like you lost work.
- git restore
- Discards working-tree changes (git restore <file>) or unstages a file (git restore --staged <file>). It doesn't touch history.
- git reset
- Moves HEAD (and the branch) to an earlier commit. --soft keeps the changes staged, --hard also discards working-tree changes. Safe on local history only.
- git revert
- Creates a new commit that reverses a previous commit's change, without rewriting history. The safe way to undo something already pushed and shared.
- git reflog
- A local log of everywhere HEAD has been. Lets you find the hash of a commit that seemed 'lost' after a reset and recover it. Your safety net.
- Shared history
- Commits that were already pushed and that others may have pulled. Rewriting them breaks the team's copy — so on them you use revert, not reset --hard.