git clean -ndX不会删除目录

时间:2013-11-11 11:17:15

标签: git

使用.gitignore

foo/

和git存储库:

./quux
./quux/foo
./quux/foo/bar
./quux/foo/bar/baz

当忽略foo时,git假定工作目录是干净的:

% git status
# On branch master
nothing to commit, working directory clean

git clean -ndx打印:

% git clean -ndx
Would remove quux/

git clean -ndX不会打印任何内容。我希望它也会删除quux/

这是一个我不知道的错误或一些gitignore功能吗? 如果我将一些文件添加到quux并提交它。比git clean会尝试按预期删除quux/foo/

我的git很新:git version 1.8.3.4

1 个答案:

答案 0 :(得分:4)

$ git init .
$ echo foo/ >.gitignore
$ git add .;git commit -am ignore
$ mkdir -p foo/bar bar/foo
$ touch foo/bar/1 bar/foo/1
$ git clean -ndx
Would remove bar/
Would remove foo/
$ git clean -ndX
Would remove foo/
$

git help clean说:

   -X
       Remove only files ignored by Git. This may be useful to rebuild
       everything from scratch, but keep manually created files.

这意味着它不会删除手动创建的内容,除非明确忽略它。

在这种情况下,bar目录是手动创建的,未明确忽略,因此不会被删除。

您的quux目录也是如此。

要“修复”此项,您需要通过添加文件在git的控制下添加bar目录:

$ touch bar/x
$ git add bar/x
$ git commit -am add\ x
$ git clean -ndX
Would remove bar/foo/
Would remove foo/
$