如何指定 git 只忽略匹配模式而不是目录的文件?

How to specify git to only ignore files that match a pattern and not a directory?

我今天 运行 遇到一个场景,其中与我们希望忽略的文件和我们的一个项目中的子目录发生命名冲突。在 git 正在建立的新项目中,有一个名为 src/core/app 的目录。我们的默认 git 忽略排除任何名为 core 的文件,因为如果有核心转储,它就会被输出。 .gitignore 文件包含类似这样的行:

#debug files
core

我们通过 git add -f 解决了这个问题,但我很好奇是否有可用的 git ignore 语法指定我们只想忽略名为 core 的文件而不包含任何文件可能命名为 core?

的目录

是的,这实际上可以使用否定模式和尾部斜杠(与目录匹配)。将这两行放在 .gitignore:

core
!core/

这将忽略名为 core 的文件,但不会忽略名为 core.

的目录