Git remove untracked files — the safe git clean ritual
git clean -nd previews, git clean -fd deletes — clean is permanent; no trash. Removes nothing? Files are in an untracked directory (add -d) or ignored (add -x). git stash -u keeps them recoverable.
Git remove untracked files comes down to a two-command ritual: git clean -nd previews, git clean -fd deletes. Clean is permanent; the trash is never involved. If clean removes nothing, the files sit in an untracked directory (add -d) or are ignored (add -x).
How do I remove untracked files safely?
Only the ?? lines are clean territory. Tracked-but-changed files get git restore or a commit; clean won't touch them.
M src/app.ts # modified: tracked, changed?? notes.txt # untracked: new file git doesn't know?? build/ # untracked directory: entirely new to git$ git clean -nd # Would remove: notes.txt build/$ git clean -fd
Preview, delete, verify — thirty seconds, zero regret, and git status finally reads clean again.
| Command | Touches | Removes from disk | Use when |
|---|---|---|---|
git clean -fd | Untracked files/dirs | Yes | Delete files git has never tracked |
git clean -fdx | Untracked + ignored | Yes | Full reset including node_modules, build output |
git rm <file> | Tracked files | Yes (staged) | Delete a file and record the deletion in git |
git rm --cached <file> | Tracked files | No | Stop tracking a file, keep it on disk |
git restore <file> | Tracked files | No | Discard local edits, keep the file |
git status --short— see what's in the tree.git clean -nd— look before you clean, the way you'dgit diffbefore you commit.- Nothing listed? Add
-dfor untracked directories,-xfor ignored files (node_modules,.venv,.env), or-ffdfor a nested repo — prefer removing the submodule properly. git clean -fd, thengit status --shortto confirm no ?? lines remain.
git clean -f — stash -u first if you might want the files back.every flag, and what never to run
git clean -fd build/ # only inside build/ git clean -fd -e notes.txt # clean everything except notes.txt git stash push --include-untracked git clean -fd git stash pop git clean -nd > clean-plan.txt
-f is required — git refuses without it. -d recurses into untracked directories; -n previews; -x also deletes ignored files; -X deletes only ignored ones; -i is interactive. Never run git clean -fdx blindly in a monorepo — it deletes every node_modules, virtualenv and local .env, and a deleted .env may not be recoverable. Never alias force into clean; keep the dry run two commands away. First published 2026-09-09, record refreshed 2026-10-04.
- tested on: git clean · the dry-run ritual · .gitignore-aware with -x
- flags: -f · -d · -n · -x · -X · -i · -e <path>
- every "removes nothing" cause — git's documented clean behaviour
- first published 2026-09-09 · record refreshed 2026-10-04