Cheat Sheets · Git Cheat Sheet · Git Discard Local Changes — Safely and Completely
Git Discard Local Changes — Safely and Completely
git restore <file>Discard changes in one file
git restore .Discard changes in the entire working tree
git restore --staged <file>Keep changes, just unstage them
Tracked files (modified but not committed)
| Command | Effect |
|---|---|
git restore <file> | Discard changes in one file |
git restore . | Discard changes in the entire working tree |
git restore --staged <file> | Keep changes, just unstage them |
Untracked files (new files Git is not watching)
| Command | Effect |
|---|---|
git clean -n | DRY RUN — list what would be deleted (always start here) |
git clean -fd | Delete untracked files and directories |
git clean -fdx | Also delete ignored files (node_modules, .env…) — extra careful |
Throwing away uncommitted work is the one Git action with no built-in undo — so it pays to know exactly which command throws away what.
Related Git recipes
FAQ
restore vs the old checkout -- ?
Same effect; restore arrived in Git 2.23 to split checkout's overloaded jobs. checkout still works but restore is clearer.
Can I recover discarded changes?
Tracked-file discards: sometimes, via git fsck --lost-found (dangling blobs) or your IDE's local history. Untracked files removed by clean: gone, unless your editor kept copies.
How do I discard only SOME changes in a file?git restore -p walks you through hunk by hunk, y/n per change.
Stop memorizing — build it interactively
Build the discard workflow in the Git BuilderFull reference: Git Cheat Sheet