clean Git Command Guide
The git clean command removes untracked files from the working tree, providing a way to remove files and directories that are not under Git version control.
git clean Syntax:
Section titled “git clean Syntax:”git clean [<options>] [--] <path>...Options:
Section titled “Options:”| Option | Description |
|---|---|
| -d | Remove untracked directories in addition to untracked files |
| -f, —force | Force clean even if clean.requireForce is true |
| -i, —interactive | Show files to be cleaned interactively |
| -n, —dry-run | Show what would be done without actually doing it |
| -q, —quiet | Be quiet, only report errors |
| -e | Add an exclude pattern |
| -x | Don’t use the standard ignore rules, still use .gitignore |
| -X | Only remove files ignored by Git |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| Restrict cleaning to specific paths or files |
git clean Command Samples:
Section titled “git clean Command Samples:”Preview what would be cleaned
Section titled “Preview what would be cleaned”git clean -nShows which files would be removed without actually deleting them.
Remove untracked files
Section titled “Remove untracked files”git clean -fForces removal of untracked files in the working directory.
Remove untracked files and directories
Section titled “Remove untracked files and directories”git clean -fdRemoves both untracked files and empty directories.
Interactive cleaning
Section titled “Interactive cleaning”git clean -iShows an interactive menu to select which files/directories to clean.
Clean only ignored files
Section titled “Clean only ignored files”git clean -fXRemoves only files that are ignored by Git (not tracked).
Clean with exclude pattern
Section titled “Clean with exclude pattern”git clean -f -e "*.log"Forces clean while excluding .log files from removal.
Clean specific directory
Section titled “Clean specific directory”git clean -fd build/Removes untracked files and directories only within the build/ directory.
How do I preview what git clean will remove?
Section titled “How do I preview what git clean will remove?”To preview what git clean will remove, use:
git clean -nHow can I force remove untracked files?
Section titled “How can I force remove untracked files?”To force remove untracked files, run:
git clean -fHow do I remove untracked directories as well?
Section titled “How do I remove untracked directories as well?”To remove untracked directories as well, execute:
git clean -fdHow can I clean interactively?
Section titled “How can I clean interactively?”To clean interactively, use:
git clean -iHow do I clean only ignored files?
Section titled “How do I clean only ignored files?”To clean only ignored files, run:
git clean -fXApplications of the git clean command
Section titled “Applications of the git clean command”- Cleaning build artifacts and temporary files in development directories
- Resetting working directory to match repository state exactly
- Preparing repository for clean commits or releases
- Removing accidentally committed files that should be ignored
- Managing multiple build configurations in project directories
- Clearing workspace before switching between branches with different file sets