Git 忽略扩展名但排除目录

Git ignore with extension but exclude directory

我有这样一个文件夹:

/<root>
|.git/
|3rd party/
|         |a.txt
|.gitignore
|a.txt

.git忽略内容:

*.txt
*.exe
*.dll
*.lib
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/

!3rd party/

我想忽略根文件夹中的某种文件,例如 exe, dll, lib, txt,所有子文件夹 除了 3rd party/ 文件夹。但是 git 仍然忽略文件 3rd party/a.txt.
这里有什么问题?

P/s: 我也希望 Debug, Release 目录中的 3rd party 文件夹(如果有)不被忽略。也就是说,此 .gitignore 文件中的每条规则都不会应用于 3rd party 文件夹

尝试这样的事情。此外,这是假设您连接了您的第 3 方文件夹,所以它是 3rdparty.

*.exe
*.txt
*.lib
!/3rdparty/*.lib
!/3rdparty/*.exe
!/3rdparty/*.txt

如果您只想查看 3rd party 目录的文本文件,请将 !3rd party/*.txt 添加到您的 .gitignore.

之后你还需要清除cache

git rm --cached .gitignore