LinuxCommandLibrary

git-clean

Remove untracked files from working directory

TLDR

Interactively delete untracked files

$ git clean [[-i|--interactive]]
copy

Show which files would be deleted without actually deleting them
$ git clean [[-n|--dry-run]]
copy

Immediately force deletion of all untracked files
$ git clean [[-f|--force]]
copy

Delete untracked [d]irectories
$ git clean [[-f|--force]] -d
copy

Delete only untracked files matching specific paths or glob patterns
$ git clean [[-f|--force]] -- [path/to/directory] '[*.ext]'
copy

Delete untracked files except those matching the given patterns
$ git clean [[-f|--force]] [[-e|--exclude]] '[*.ext]' [[-e|--exclude]] [path/to/directory]/
copy

Delete untracked files and e[x]cluded files (those listed in .gitignore and .git/info/exclude)
$ git clean [[-f|--force]] -x
copy

SYNOPSIS

git clean [-d] [-e<pattern>] [--exclude=<pattern>] [-f|--force] [-i|--interactive] [-n|--dry-run] [-q|--quiet] [-X] [-x] [--ignored] [--ignore-submodules] [--one-file] [--standard] [<path>...]

PARAMETERS

-d
    Remove untracked directories too.

-e <pattern>, --exclude=<pattern>
    Exclude files matching the pattern from removal.

-f, --force
    Force removal; required unless disabled in config.

-i, --interactive
    Interactively select files to remove.

-n, --dry-run
    Preview files without deleting.

-q, --quiet
    Suppress output and ignore errors.

-X
    Remove only files ignored by Git.

-x
    Remove all untracked files, ignoring .gitignore.

--ignored
    Show only ignored files.

--ignore-submodules
    Skip cleaning submodules (default).

--one-file, --standard
    Emulate Unix rm behavior for unreadable files.

DESCRIPTION

git clean removes untracked files and directories from the Git working tree. Untracked items are those not added to the index or committed, including build artifacts, temporary files, or ignored content.

By default, it requires -f to force deletion, preventing accidental data loss. Use -n for a dry-run preview showing what would be removed without action. The -d option includes untracked directories; -x ignores .gitignore rules to remove all untracked files; -X removes only ignored files.

Interactive mode (-i) lets users selectively clean files. It operates on the entire tree unless paths are specified. Configurable via clean.requireForce to enforce safety checks.

This command is powerful for maintaining a clean workspace but destructive—deleted files are gone permanently unless backed up. Always dry-run first.

CAVEATS

Destructive: deletes files permanently. Use -n first. Ignores Git-tracked files; submodules may need separate handling.

EXAMPLES

git clean -n (preview)
git clean -fd (force directories)
git clean -fdx (all files)

CONFIG

clean.requireForce: true enforces -f/-n/-i; false allows default run.

HISTORY

Introduced in Git 1.5.0 (February 2007). Interactive mode added in 1.7.0 (2010); options like -x refined for flexibility.

SEE ALSO

git-status(1), git-add(1), git-reset(1), gitignore(5), rm(1)

Copied to clipboard