git-clean
Remove untracked files from working directory
TLDR
Delete untracked files
Interactively delete untracked files
Show which files would be deleted without actually deleting them
Forcefully delete untracked files
Forcefully delete untracked [d]irectories
Delete untracked files, including e[x]cluded files (files ignored in .gitignore and .git/info/exclude)
SYNOPSIS
git clean [-d] [-f | -i] [-n] [-q] [-e
PARAMETERS
-f, --force
Force removal of untracked files and directories. This option is required unless the configuration variable clean.requireForce is set to false.
-n, --dry-run
Show what would be done without actually removing anything. This is highly recommended for safety.
-d
Remove untracked directories in addition to untracked files.
-x
Do not consult any ignore files (like .gitignore). Remove all untracked files and directories, including those normally ignored by Git.
-X
Remove only files that are ignored by Git. This is useful for cleaning up build artifacts or generated files that are typically listed in .gitignore.
-i, --interactive
Run in interactive mode. Git will show what would be done and ask for confirmation before each removal.
-q, --quiet
Suppress reporting of files removed. Only error messages will be shown.
-e
Add a temporary exclude pattern. Files matching this pattern will not be removed or shown in the dry run, similar to an entry in .gitignore.
--
Limit the cleaning operation to specific paths within the working directory. Untracked files/directories outside these paths will not be affected.
DESCRIPTION
The git clean command is used to remove untracked files and directories from your working tree. It helps in restoring your working directory to a clean state, similar to a fresh clone, by deleting files that are not part of your Git repository and are not listed in your .gitignore.
This command is powerful and destructive; once files are removed, they are gone permanently. For safety, it requires the -f (force) option to execute, and it's highly recommended to always perform a --dry-run (-n) first to see what would be removed.
CAVEATS
Destructive Nature:
git clean permanently deletes files and directories from your working tree. There is no undo mechanism within Git for these operations. Always use the -n (dry-run) option first to preview what will be removed.
Force Requirement:
The -f or --force option is almost always required to perform any cleaning, serving as a critical safeguard against accidental data loss.
Does Not Affect Tracked Files:
This command only operates on untracked files and directories. Files that are tracked by Git (i.e., part of your repository history) are never touched by git clean.
Ignored Files:
By default, git clean will not remove files listed in your .gitignore. To remove all untracked files including ignored ones, use -x. To remove only ignored files, use -X.
COMMON USAGE PATTERNS
Here are some typical ways to use git clean safely and effectively:
Preview what would be removed (files only):git clean -n
Preview what would be removed (files and directories):git clean -fdn
Preview what would be removed (all untracked, including ignored):git clean -xfdn
Execute removal of untracked files only:git clean -f
Execute removal of untracked files and directories:git clean -fd
Execute removal of all untracked files and directories, including ignored ones:git clean -xfd
HISTORY
The git clean command has been a part of Git since its early days, fulfilling the essential role of helping developers maintain a clean working directory by removing transient or unnecessary files that are not part of the repository's tracked history. Its core functionality has remained consistent, with minor enhancements over time to improve control and safety, such as the addition of interactive mode and more granular ignore options.