git 忽略除一种类型的文件以外的所有文件,在任何位置

git ignore all but one type of file, anywhere down

我的目标是编写一个 .gitignore 文件,它将忽略任何向下级别的所有内容,除了:

*.xxx
*.yyy

在当前目录中(.gitignore 所在的位置)和

*.xxx

任何地方 - 在任何级别 - 在下面的树中。

换句话说,我想在任何级别保持git *.xxx 以及在'root' 级别保持*.yyy。其他任何东西都应该被忽略。

子文件夹的名称未知,因此我无法在git忽略文件中引用它们。

我尝试了很多替代方案,但到目前为止都没有成功: 例如:

#ignore everything in any subdirectory
*
*/**
#expect some files:
!*.yyy
!*/**/*.xxx

我想我达到了限制:

"It is not possible to re-include a file if a parent directory of that file is excluded"

在 git 中忽略文档...有没有办法解决这个...?

问题确实在于一开始将所有子目录列入黑名单,然后试图重新包含这些目录中的某些内容。

此问题的解决方案是在尝试重新包含文件之前再次将子目录列入白名单:

# blacklist everything
*

# whitelist all content ending with slash (directories)
!/**/

# finally whitelist all files with valid extension in the entire repo
!*.xxx
!*.yyy
# ignoring the ones that are only allowed within the repo root
*/**/*.yyy