Discard tracked changes
git restore is the modern command; git checkout -- does the same thing. Both overwrite the working file with the index or HEAD version, so any uncommitted edits are gone.
git restore file.txt # discard working copy changes
git restore . # discard all tracked changes
git restore --staged file.txt # unstage, keep working copy
# legacy syntax, still common in docs
git checkout -- file.txt
Remove untracked files
git clean deletes files Git does not track. It is destructive and does not respect .gitignore unless you pass -x. Always preview with -n first.
git clean -nd # preview files and directories
git clean -fd # remove untracked files and directories
git clean -fdx # also remove ignored files (careful: build artifacts, .env)
-x removes ignored files such as .env, local databases and build caches. On a working machine, that can be far more destructive than the tracked changes you meant to discard.
Discard everything at once
This returns the repository to the last commit, deleting tracked modifications and untracked files. Untracked-but-ignored files survive unless -x is used. There is no undo for the uncommitted work.
git reset --hard HEAD
git clean -fd
If you discarded too much
- Committed changes: recover via
git reflog(the commit still exists). - Staged changes:
git fsck --lost-foundmay find dangling blobs, but recovery is unreliable. - Unstaged edits: effectively gone. Check your editor's local history or backups.
- Untracked files: gone unless the OS has a snapshot (Time Machine, VSS, etc.).
Before any destructive command, run git status and git diff --stat. If the output surprises you, stop.