Remove untracked files from a Git working tree
Preview git clean before removing untracked files, choose whether ignored files belong in the cleanup, and keep nested repositories and backups in mind.
Use git clean -nd to preview untracked files and directories in the current directory's scope. After reviewing the list, run git clean -fd to remove them. Ordinary ignore rules apply unless you choose a broader cleanup.
git clean -nd
git clean -fd
Run from the repository root if you intend to inspect the whole checkout. A dry run shows the current candidates; it is not a backup and cannot protect a file created or changed between the preview and deletion.
Choose the scope carefully
| Option | Effect |
|---|---|
-n |
Show candidates without deleting them |
-f |
Permit removal under the usual force requirement |
-d |
Include untracked directories when no pathspec is supplied |
-x |
Include ignored files as well as ordinary untracked files |
-X |
Remove only ignored files |
-i |
Use interactive selection |
-e PATTERN |
Add an exclusion for this command |
When an explicit pathspec is supplied, it determines the matching scope and -d is not required to recurse into matching untracked directories. Git normally protects nested untracked repositories; a second force option can override that protection. Do not add it just to silence a warning.
Preview ignored-file cleanup
# Inspect ignored candidates only.
git clean -ndX
# Inspect both ignored and ordinary untracked candidates.
git clean -ndx
Ignored does not mean disposable. The list can include credentials, local databases, dependencies, or generated artifacts you cannot easily reproduce. Only replace -n with -f after deciding what may be removed.
To retain a root .env while inspecting broader cleanup:
git clean -ndx -e /.env
Keep the same exclusions when executing. Without -x, an already ignored .env is normally excluded anyway.
Understand how reset differs
git reset --hard HEAD resets tracked contents and the index, whereas git clean removes selected untracked contents. Combining them can discard both kinds of local work. It still does not recreate a fresh clone: refs, configuration, retained metadata, and protected nested repositories may remain.
FAQ
Can Git recover a cleaned file?
Not if Git never stored it and no other copy exists. Check a prior commit, stash, editor history, or backup when relevant. Save ordinary untracked work with git stash push -u before cleanup if you need it; ignored files require a different scope or separate backup.
Why does clean refuse without -f?
The normal clean.requireForce setting requires confirmation through a force option. Dry-run and interactive modes provide their own protections. Keeping the default is useful; changing it is not necessary for routine cleanup.
Does this remove modified tracked files?
No. A tracked file's modifications are a separate restore/reset decision. Inspect git status to distinguish the two cases.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan