如何为我们的 Git 存储库中的特定文件夹创建忽略规则例外?

How do i create an ignore rules exception for a specific folder in our Git repository?

在我们项目文件夹层次结构的顶层,我们创建了标准忽略规则来排除我们编译的 exe、dll 等。由于这些规则是在我们存储库的顶层指定的,因此它们对项目层次结构中的所有文件夹。

我有一个包含多个 exe 的特定文件夹,我想将它们与我们的 Git 存储库同步。我如何关闭子文件夹级别的排除,以便该文件夹中的 exe 将与我们的 Git 存储库同步? (子文件夹是 'leaf',所以一旦排除被禁用,它可以保持原样。)

答案似乎很简单:

!*.exe

!表示反向忽略规则。见 https://git-scm.com/docs/gitignore#_pattern_format:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

您可以直接在目录中添加规则(我们称之为 path/to/the/leaf),即在文件 path/to/the/leaf/.gitignore 中。或者您可以将其添加到顶层 .gitignore 中,形式为:

!/path/to/the/leaf/*.exe

忽略后再添加*.exe

# ignore all . files and . folders
.*

# Dont ignore .gitignore (this file)
# This is just for verbosity, you can leave it out if
# .gitignore is already tracked or if you use -f to
# force-add it if you just created it
!/.gitignore

# ignore all . folders but include . files
.*/

What is this pattern?

.* - 这个模式告诉 git 忽略所有以 .

开头的文件

! - 这告诉 git 不要忽略模式。你的情况 /.gitignore