Git忽略了父目录中的.gitignore文件

5

我有一个类似这样的结构:

projects/
    .gitignore
    foo/
        some_file
    bar/
        another_file
foobarprojects是git项目。在.gitignore中,我写了*(实际上我尝试过很多东西),但是当我在projects/foo中运行git status时,它仍然告诉我要添加some_file。如果我正确地阅读了.gitignore文档,它说Git应该在所有父文件夹中查找.gitignore文件并使用它们。但是它似乎完全忽略了.gitignore中的那个。这是怎么回事?
我已经尝试了git check-ignore,但它好像根本不存在.gitignore
复现步骤:
/ $ cd /tmp
/tmp $ mkdir projects
/tmp $ cd projects
/tmp/projects $ echo "*" > .gitignore
/tmp/projects $ git init
Initialized empty Git repository in /private/tmp/projects/.git/
/tmp/projects $ mkdir foo
/tmp/projects $ cd foo
/tmp/projects/foo $ git init
Initialized empty Git repository in /private/tmp/projects/foo/.git/
/tmp/projects/foo $ touch some_file
/tmp/projects/foo $ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    some_file

nothing added to commit but untracked files present (use "git add" to track)

你确定 .git/ 目录放在正确的位置吗? - iBug
哪个.git/目录? - Timmmm
3个回答

4

Git仅使用当前代码库中.gitignore文件中的规则。如果您希望将这些规则应用于所有子代码库,可以将父目录中的.gitignore文件建立符号链接或硬链接到所有子目录中。


2

Git 2.33 (2021年第三季度)明确了对.gitignore的检测:

请参见commit e041706 (2021年7月6日),作者为Andrew Berry (deviantintegral)
(由Junio C Hamano -- gitster --commit bb3a55f合并,于2021年7月22日)

文档: .gitignore解析在仓库的顶层

签名作者:安德鲁·贝瑞

当前文档描述了.gitignore文件将在每个父目录中解析,直到它们达到仓库边界为止。
这澄清了当前的行为。

此外,这纠正了“toplevel”为“top-level”,与“top-level domain”的用法相匹配。

gitignore现在在其手册页中包含:

读取从 .gitignore 文件中的模式与路径相同的目录或任何父级目录(直到工作树的顶级)中的模式,高级别文件中的模式被低级别文件中的模式覆盖,直到包含该文件的目录。

这些模式与 .gitignore 文件的位置相关。

由于您在 /private/tmp/projects/foo/.git/ 中初始化了嵌套的 Git 存储库,因此将忽略 projects/ 中的 .gitignore 文件本身。


0

哦... 它只读取它们到git存储库的顶层

从与路径相同目录中的.gitignore文件读取的模式,或者在任何父目录中,其中高级别文件中的模式(直到工作树的顶层)

措辞不太好,但还可以。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接