mrsn0.3.0
auth
live

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.

2026-11-232 min read

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
zones in the tree
3
tracked · untracked · ignored
refusals to a plain clean
3
untracked dir · ignored · nested repo
the safety rail
-n
--dry-run, always first
Preview, delete, verify — thirty seconds, zero regret, and git status finally reads clean again.
CommandTouchesRemoves from diskUse when
git clean -fdUntracked files/dirsYesDelete files git has never tracked
git clean -fdxUntracked + ignoredYesFull reset including node_modules, build output
git rm <file>Tracked filesYes (staged)Delete a file and record the deletion in git
git rm --cached <file>Tracked filesNoStop tracking a file, keep it on disk
git restore <file>Tracked filesNoDiscard local edits, keep the file
  1. git status --short — see what's in the tree.
  2. git clean -nd — look before you clean, the way you'd git diff before you commit.
  3. Nothing listed? Add -d for untracked directories, -x for ignored files (node_modules, .venv, .env), or -ffd for a nested repo — prefer removing the submodule properly.
  4. git clean -fd, then git status --short to confirm no ?? lines remain.
The law: clean manages what git doesn't know about; rm and restore manage what it does. No undo after 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
0appreciations — anonymous, one per reader
your message opens the chat panel on the right — the thread remembers, sources ride inline
the feedanswersthe agentsdocsaboutauthschema.jsonopenapi.jsonllms.txt