git clean无法清除隐藏文件

4

我在工作目录中有3个未跟踪的文件:

$ git sta
## features/subchecks...upstream/BR_4_1_4 [ahead 4]
?? Applications/ETM/Build/Android/.project
?? Applications/zFacebook/Build/Android/.project
?? Core/Barcode/Build/Android/.project

我会先试运行 git clean 命令,步骤如下:

$ git clean -n

但是它没有显示将要被清理的任何文件。为什么它没有清理.project文件呢?我正在使用Windows上的msysgit 1.9.2。

3个回答

6

要将树形结构还原为完全干净的状态并且清除所有本地未被git跟踪的内容,请执行以下操作:

git clean -fdx

如果您想保留被 Git 忽略的文件(即在 .gitignore 中的文件),则不要加上 "x"。

3

这是因为你需要在git clean后面添加-d参数,以便清除目录:

% git help clean

OPTIONS
       -d
           Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not
           removed by default. Use -f option twice if you really want to remove such a directory.

我无法指定d选项,因为这样它会说正在清理比必要更多的文件。只包含被忽略文件(但目录本身没有被忽略)的目录将被标记为删除,而我不想这样。基本上,应该删除出现在git状态中的每个未跟踪的文件和/或目录,没有其他更多的东西。 - void.pointer
那么你想要的是@RobertDailey的解决方案。 - zmo
@zmo我在哪里可以找到这样的解决方案? - Qwerty

2

使用git clean无法实现此目的,但以下方法对我有效且只清除了在git状态中看到的内容:

$ rm $(git ls-files --others --exclude-standard)

如果你想让该命令支持空格,你可以使用 git ls-files --exclude-standard -oz | xargs -0 rm -f - Erik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接