Git Clean Untracked Files Guide
When to Use
When many untracked files or directories appear in the repository -- build artifacts, temporary files, downloaded data, test outputs -- and you're sure you don't need them, use git clean.
This page handles:
- Untracked files
- Untracked directories
- Optionally, ignored files
It does not modify files already tracked by Git.
Check Current State First
git status --short
git clean -nd
git clean -ndX
git clean -nd-- preview only, no deletion. Run this first.git clean -ndX-- preview only what "ignored files" would be deleted.
Recommended Commands
| Scenario | Recommended Command | Result |
|---|---|---|
| Preview untracked files to be deleted | git clean -nd | Safe preview, no deletion |
| Delete untracked files and directories | git clean -fd | Clean up normal untracked content |
| Delete ignored files too | git clean -xfd | Most thorough, also most dangerous |
| Interactive selection for deletion | git clean -id | Confirm each item, more cautious |
Common Scenarios
Preview First, Then Delete Untracked Content
git clean -nd
git clean -fd
Only Clean Ignored Build Artifacts
git clean -fdX
-X means "only delete ignored files" -- suitable for cleaning dist/, build/, .cache/, etc.
Restore Repository to a Very Clean State
git restore .
git clean -fd
If there are staged changes, run first:
git restore --staged .
Risks and Boundaries
git cleandeletes real files on disk -- they don't go to the recycle bin.git clean -xfdwill also delete ignored files, commonly things likenode_modules/,.env, and build cache directories.- By default,
git cleanrequires-fto actually execute -- this is an intentional Git safeguard. - It does not handle modifications to tracked files; for that, see Discard Working Directory Changes.
Related Commands
git status --short --ignored
git clean -nd
git clean -fdX
git stash push -u -m "backup before clean"