Skip to content

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.

Terminal window
git clean [<options>] [--] <path>...
OptionDescription
-dRemove untracked directories in addition to untracked files
-f, —forceForce clean even if clean.requireForce is true
-i, —interactiveShow files to be cleaned interactively
-n, —dry-runShow what would be done without actually doing it
-q, —quietBe quiet, only report errors
-e , —exclude=Add an exclude pattern
-xDon’t use the standard ignore rules, still use .gitignore
-XOnly remove files ignored by Git
ParameterDescription
Restrict cleaning to specific paths or files
Terminal window
git clean -n

Shows which files would be removed without actually deleting them.

Terminal window
git clean -f

Forces removal of untracked files in the working directory.

Terminal window
git clean -fd

Removes both untracked files and empty directories.

Terminal window
git clean -i

Shows an interactive menu to select which files/directories to clean.

Terminal window
git clean -fX

Removes only files that are ignored by Git (not tracked).

Terminal window
git clean -f -e "*.log"

Forces clean while excluding .log files from removal.

Terminal window
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:

Terminal window
git clean -n

To force remove untracked files, run:

Terminal window
git clean -f

How do I remove untracked directories as well?

Section titled “How do I remove untracked directories as well?”

To remove untracked directories as well, execute:

Terminal window
git clean -fd

To clean interactively, use:

Terminal window
git clean -i

To clean only ignored files, run:

Terminal window
git clean -fX
  1. Cleaning build artifacts and temporary files in development directories
  2. Resetting working directory to match repository state exactly
  3. Preparing repository for clean commits or releases
  4. Removing accidentally committed files that should be ignored
  5. Managing multiple build configurations in project directories
  6. Clearing workspace before switching between branches with different file sets