This informational message appears during a git clean dry run, indicating that an untracked directory would be deleted. Use the -d flag with -f to actually remove directories, or review the output to ensure you won't lose important files.
When you run `git clean -n` (dry run mode), Git shows you what it would delete without actually deleting anything. The "Would remove directory/" message indicates that an untracked directory exists in your working tree and would be removed if you ran the actual clean command. By default, `git clean` only removes untracked files, not directories. This is a safety mechanism to prevent accidentally deleting entire folder structures that may contain important work. To include directories in the clean operation, you must explicitly add the `-d` flag. The "Would remove" output is not an error—it's Git's way of giving you a preview before making potentially destructive changes. This allows you to review what will be deleted and abort if necessary. Since `git clean` permanently removes files that aren't tracked by Git, there's no way to recover them through Git (unlike tracked files which can be restored via `git checkout` or `git restore`).
First, run a dry run to see exactly what would be deleted:
git clean -nThis shows files only. To include directories in the preview:
git clean -dnReview the output carefully. The "Would remove" prefix indicates files and directories that will be permanently deleted.
If you only want to remove untracked files but keep directories:
git clean -fThe -f (force) flag is required because Git's default configuration (clean.requireForce) prevents accidental deletions.
To remove both untracked files and directories, combine the -d and -f flags:
git clean -dfThis will delete:
- All untracked files
- All untracked directories (and their contents)
Warning: This action is irreversible. Files removed by git clean cannot be recovered through Git.
For more control over what gets deleted, use interactive mode:
git clean -diThis presents a menu where you can:
- clean: Remove all listed items
- filter by pattern: Exclude files matching a pattern
- select by numbers: Choose specific items to delete
- ask each: Confirm each deletion individually
- quit: Exit without deleting anything
- help: Show available commands
To also remove files that are ignored by .gitignore (like build artifacts):
# Preview what will be removed (including ignored files)
git clean -dnx
# Actually remove (including ignored files)
git clean -dfxTo remove ONLY ignored files (keeping untracked non-ignored files):
git clean -dfXThis is useful for cleaning build outputs while keeping your untracked configuration files.
To exclude certain files from being cleaned:
# Exclude a specific file
git clean -df -e "important-file.txt"
# Exclude files matching a pattern
git clean -df -e "*.log"
# Multiple exclusions
git clean -df -e "*.config" -e "local-settings/"This is helpful when you want to clean most untracked files but preserve certain ones.
If git clean encounters untracked directories that contain .git subdirectories (nested repositories), it will refuse to remove them by default. To force removal:
git clean -dffThe double -f is required to delete nested git repositories. Be very careful with this—you could lose an entire repository's history.
If you're unsure about permanently deleting files, consider using git stash instead:
# Stash untracked files (can be restored later)
git stash --include-untracked
# Or stash everything including ignored files
git stash --allUnlike git clean, stashed changes can be retrieved with:
git stash pop### Git Clean Flag Reference
| Flag | Description |
|------|-------------|
| -n, --dry-run | Preview only, don't delete anything |
| -f, --force | Required to actually delete files |
| -d | Include untracked directories |
| -x | Also remove ignored files |
| -X | Remove ONLY ignored files |
| -i, --interactive | Interactive mode for selective deletion |
| -e <pattern> | Exclude files matching pattern |
| -q, --quiet | Only report errors |
### Why Git Requires Force
The clean.requireForce configuration option (default: true) exists because git clean is destructive. Unlike most Git operations, files removed by git clean cannot be recovered—they're not tracked, so there's no commit to restore from.
To disable this protection (not recommended):
git config --global clean.requireForce false### Cleaning in CI/CD Pipelines
In build pipelines, a common pattern is:
# Reset tracked files and clean everything
git reset --hard HEAD
git clean -dfxThis ensures a pristine working directory matching the repository state exactly. However, be aware this removes node_modules, build caches, and other files that may slow down builds.
### Working Directory Scope
git clean only operates on the current directory and its subdirectories. To clean the entire repository:
cd $(git rev-parse --show-toplevel)
git clean -df### Empty Directories
Git doesn't track empty directories. Sometimes git clean -dn shows directories that don't appear in git status. This is because git status focuses on files, while git clean with -d explicitly looks for untracked directories.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server