Synopsis
git clean [-d] [-f] [-x | -X] [-n] [-i] [<path>...]
Description
The git clean command removes untracked files from the working tree. By default it requires -f (or clean.requireForce = false) as a safety measure because deletions are irreversible. With -d it also removes untracked directories, and -x additionally removes files that are normally ignored (build outputs, dependency caches).
Always run git clean -n first to preview which files will be deleted. The interactive mode (-i) lets you confirm or filter the list. git clean is the right tool for resetting a project to a pristine state, especially before debugging build issues.
In day-to-day use, git clean integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git clean --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git clean interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git clean's side effects helps avoid all three.
Common Options
| Option | Description |
|---|---|
-f, --force | Required to actually delete (unless config disables this). |
-n, --dry-run | Show what would be deleted, don't delete. |
-d | Also remove untracked directories. |
-x | Also remove ignored files. |
-X | Remove only ignored files. |
-i, --interactive | Interactive selection. |
-e <pattern> | Exclude files matching pattern. |
Examples
git clean -nd
# Preview what would be deleted (dirs included)
git clean -fd
# Actually delete untracked files and directories
git clean -fdx
# Plus ignored files (full reset to pristine)
git clean -i
# Interactive: choose what to delete
Common Mistakes
git clean -fdx wipes node_modules, build artifacts, IDE settings, and even .env files if they're gitignored. Always preview with -n first. Be especially careful in submodules — clean operates per-repository.
Related Commands
git status, git stash, git reset, git rm