Git clean Command
In this page:
Dry-Running Clean
git clean permanently deletes untracked files from your working directory -- since this can't be undone, always run git clean -n (dry run) first to see exactly which files would be removed before actually removing anything.
Example: Dry-Running Clean
git clean -n
Deleting Untracked Files
Once you've confirmed the dry-run list looks right, git clean -f (force) performs the actual deletion of every untracked file it listed -- Git requires this explicit force flag as a safeguard against accidental data loss.
Example: Deleting Untracked Files
git clean -f
Cleaning Untracked Directories
By default, git clean only removes loose files and leaves now-empty folders behind; adding -d extends it to also remove untracked directories, giving you a genuinely clean working tree.
Example: Cleaning Untracked Directories
git clean -fd
Cleaning Ignored Files
Files matched by your .gitignore (like build artifacts or dependency caches) are skipped by default even during a clean; the -x flag overrides that and deletes ignored files too, useful for a true from-scratch rebuild.
Example: Cleaning Ignored Files
git clean -fx
Interactive Cleaning
git clean -i opens an interactive menu instead of deleting everything at once, letting you review, filter by pattern, or select individual files -- the safest way to clean when you're not fully sure what's untracked.
Example: Interactive Cleaning
git clean -i
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)