Git refuses to run 'git clean' without a safety flag. This is an intentional safeguard to prevent accidental deletion of untracked files. Add -f, -n, or -i to proceed.
This error occurs when you run `git clean` without specifying one of the required safety flags. Git's `clean.requireForce` configuration defaults to `true`, which means Git will refuse to delete untracked files unless you explicitly confirm your intention. This is a safety feature, not a bug. The `git clean` command permanently removes untracked files from your working directory. Unlike tracked files, untracked files are not stored in Git's history, so once deleted, they cannot be recovered through Git. The requireForce setting ensures you don't accidentally delete important files like local configuration, generated assets, or work-in-progress code. The error message tells you exactly what flags you can use: `-i` for interactive mode, `-n` for a dry run (preview), or `-f` to force the deletion.
Before deleting anything, use the -n flag to see what would be removed:
git clean -nThis performs a "dry run" and lists all untracked files that would be deleted without actually removing them. Review this list carefully before proceeding.
Once you've verified the files to be removed, use the -f flag to proceed:
git clean -fThis will permanently delete all untracked files in the current directory and subdirectories. This action cannot be undone through Git.
For more control, use the -i flag to enter interactive mode:
git clean -iThis presents a menu where you can:
- Clean all files at once
- Filter by pattern to exclude certain files
- Select specific files by number
- Review each file individually before deletion
By default, git clean only removes untracked files, not directories. To also remove untracked directories:
# Dry run including directories
git clean -n -d
# Force remove files and directories
git clean -f -dBe especially careful with -d as it can remove entire folder structures.
To also remove files ignored by .gitignore:
# Remove untracked AND ignored files
git clean -f -x
# Remove ONLY ignored files (keep other untracked files)
git clean -f -XWarning: The -x flag will remove build artifacts, node_modules, IDE settings, and other ignored files. Use with caution.
Common flag combinations:
# Preview all untracked files and directories
git clean -n -d
# Remove all untracked files and directories
git clean -f -d
# Full reset: remove untracked and ignored files/directories
git clean -f -d -x
# Interactive mode with directories
git clean -i -dIf you want to clean untracked files but keep the option to recover them:
# Stash untracked files (can be recovered later)
git stash push --include-untracked -m "cleanup untracked files"
# Later, if you need them back:
git stash pop
# Or permanently remove the stash:
git stash dropThis is safer than git clean as files can be recovered from the stash.
Disabling requireForce (not recommended): You can disable this safety check by setting:
git config --global clean.requireForce falseHowever, this is strongly discouraged as it increases the risk of accidentally deleting important files.
CI/CD considerations: In automated scripts, always use git clean -f explicitly rather than disabling requireForce. This makes the script's intent clear and maintains safety in interactive use.
Checking current configuration: To see if requireForce is explicitly set:
git config --get clean.requireForceIf nothing is returned, it defaults to true.
Excluding specific files: Use the -e flag to exclude files matching a pattern:
git clean -f -e "*.log" -e "config.local.json"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