保留一些 git 忽略的文件,同时 运行 git clean -X

Preserve some gitignored files while running git clean -X

我正在使用您经常希望进行干净构建的存储库。这会导致构建脚本发出 git clean -Xdff,这将删除所有与 .gitignore 中的模式匹配的文件。我希望一些被 Git 忽略的文件(例如来自 Eclipse 的 .project)在清理后保留下来。

我尝试为 "exclude" 添加 -e 标志,但这似乎无济于事,因为我们有意清理 Git 忽略的内容。

亲身体验:

~$ mkdir testrepo && cd testrepo
~/testrepo$ git init
Initialized empty Git repository in /home/turbulencetoo/testrepo/.git/
~/testrepo$ echo ".project" > .gitignore
~/testrepo$ touch .project
~/testrepo$ git clean -Xn
Would remove .project
~/testrepo$ git clean -Xn -e .project
Would remove .project

为了在干净的构建过程中保留我的 .project 文件,您会建议对我的本地构建脚本中的 git clean 调用(或周围代码)进行哪些更改?

如@turbulencetoo所述,警告此解决方案

如果您还没有添加其他未跟踪文件(例如 newSourceFile.c),将会产生删除它们的副作用。


我认为你的问题是你使用的是 X 而不是 x 标志,你应该使用这个,

git clean -xn -e .project

git clean 开始,只有 -x 允许 -e 个文件被 考虑
意思是 .project 将被删除(-X 忽略规则或 -x -e

if the -x option is specified, ignored files are also removed.
-e <pattern>: In addition to those found in .gitignore (per directory) and $GIT_DIR/info/exclude, also consider these patterns to be in the set of the ignore rules in effect.
-X: Remove only files ignored by Git

如评论所述,-e 的文档令人困惑。