← 返回命令列表

Linux command

git-clean 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Preview files to remove

git clean -n

Remove untracked files

git clean -f

Remove untracked directories

git clean -fd

Remove ignored files

git clean -fX

Interactive clean

git clean -i

说明

git clean removes untracked files from the working tree. It cleans up generated files, build artifacts, and other files not tracked by git. The command requires -f to actually delete files, preventing accidental data loss. Dry run mode (-n) shows what would be removed without deleting anything. Options control whether ignored files (-x/-X) and directories (-d) are included. Combined with git reset --hard, git clean provides a complete way to return the working tree to a pristine state matching the last commit. Always preview with -n first, as deleted untracked files cannot be recovered through Git.

参数

-n, --dry-run
Show what would be removed.
-f, --force
Actually remove files.
-d
Remove untracked directories.
-x
Remove ignored files too.
-X
Remove only ignored files.
-i, --interactive
Interactive mode.
-e _PATTERN_
Exclude pattern.
--help
Display help information.

FAQ

What is the git-clean command used for?

git clean removes untracked files from the working tree. It cleans up generated files, build artifacts, and other files not tracked by git. The command requires -f to actually delete files, preventing accidental data loss. Dry run mode (-n) shows what would be removed without deleting anything. Options control whether ignored files (-x/-X) and directories (-d) are included. Combined with git reset --hard, git clean provides a complete way to return the working tree to a pristine state matching the last commit. Always preview with -n first, as deleted untracked files cannot be recovered through Git.

How do I run a basic git-clean example?

Run `git clean -n` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -n, --dry-run do in git-clean?

Show what would be removed.