如何从当前工作树中删除未追踪的本地文件?


当前回答

用途: git clean [-d] [-f] [-i] [-n] [-q] [-e ] [-x♰ -X] [--]...

-q, --quiet           do not print names of files removed
-n, --dry-run         dry run
-f, --force           force
-i, --interactive     interactive cleaning
-d                    remove whole directories
-e, --exclude <pattern>
                      add <pattern> to ignore rules
-x                    remove ignored files, too
-X                    remove only ignored files

其他回答

要删除未追踪的文件,您应该首先使用命令查看将受清洁影响的文件。

git clean -fdn

这将向您展示将被删除的文件列表. 现在实际上删除这些文件使用这个命令:

git clean -fd

取消完整的更改 git clean -f -d

$ git clean -f -d
Removing client/app/helpers/base64.js
Removing files/
Removing package.json.bak

where 
-f is force 
-d is a directory 

注意: 首先,导航到目录并检查您要清理的分支。

-i 互动模式,它会告诉你什么将被删除,你可以从列表中选择一个操作。

清理文件只有 [文件不会列出并不会清理]: $ git clean -i 清理文件和文件夹: $ git clean -d -i

D 包括目录。


如果您从列表中选择 c 将删除未跟踪的文件/文件夹,并将删除您删除的文件/文件夹。

例如: 如果您在远程中重组文件夹并将更改拖到本地计算机上,则由其他人创建的文件/文件夹将最初在过去的文件夹和您重组的新文件夹中。

这是我一直使用的:

git clean -fdx

对于一个非常大的项目,你可能想运行它几次。

git add --all, git stash and git stash drop, try these three commands in this order in order to remove all untracked files. 通过添加所有这些未追踪的文件到 git 并 stashing 它们将移动所有这些未追踪的文件到 stash 列表并下载顶部一个,即, stash@{0} 将从 stash 列表中删除所有未追踪的文件。