Git - 如何从单个项目的全局 .gitignore 中删除排除模式

Git - How to remove exclusion patterns from global .gitignore for a single project

假设我的全局 .gitignore 中有“.foo”,这对我当前的所有项目都非常有用(假设有很多项目,所以我想将其保留在我的全局忽略文件)。

现在,我开始一个新项目,到处都使用 .foo 个文件。

如何 "undo" 排除在我的全局忽略中定义的 .foo 文件,仅针对此特定项目?

这意味着我可以 git add . 在处理新项目时让它获取所有 .foo 文件,但是如果我 运行 git add . 在任何其他项目则忽略 .foo 文件。

每个存储库还有一个本地 .gitignore,您可以修改它。

此处接受的答案告诉您在哪里查看:

How do I configure git to ignore some files locally?

要取消忽略,只需将其添加到本地 .gitignore 文件中,并带有前导!

您可以通过添加前导 ! 来简单地忽略模式,如下所示:

# Don't ignore .foo file
!.foo

如果您将它放在本地 .gitignore 中,它将覆盖在全局 .gitignore 中完成的设置。

您可以在 gitignore 官方文档 here

中找到支持模式的文档

你要找的是否定。参见 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.

对于您的情况,只需将其添加到项目的 .gitignore 中:

!.foo

查看此问题以获得更详尽的解释: Make .gitignore ignore everything except a few files


请注意文档中的重要警告:

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

因此,如果您尝试仅将特定文件包含在排除的目录中,则必须进行更改以包含该目录,但排除其中的所有文件,除了您想要的文件:

!excludedir
excludedir/*
!excludedir/*.foo