【问题标题】:Git ignoring .gitignore file in parent directoryGit忽略父目录中的.gitignore文件
【发布时间】:2017-12-11 15:16:03
【问题描述】:

我有这样的结构:

projects/
    .gitignore
    foo/
        some_file
    bar/
        another_file

foobar projects 是 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/ 目录在正确的位置吗?
  • 哪个.git/目录?

标签: git gitignore


【解决方案1】:

Git 仅使用.gitignore 文件当前存储库中 中的规则。如果您希望将.gitignore 文件从父目录应用到所有子存储库,您可能需要符号链接或硬链接到所有子目录。

【讨论】:

    【解决方案2】:

    使用 Git 2.33(2021 年第三季度)澄清了 .gitignore 检测:

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

    docs: .gitignore 解析到 repo 的顶部

    签字人:Andrew Berry

    当前文档看起来好像.gitignore 文件将在每个父目录中被解析,直到它们到达存储库边界。
    这阐明了当前的行为。

    同样,这会将“顶级”更正为“顶级”,匹配“顶级域”的用法。

    gitignore 现在包含在其man page 中:

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

    这些模式 相对于.gitignore 文件的位置匹配。

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

    【讨论】:

      【解决方案3】:

      哦...它只会读取它们up to the top level of the git repository

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

      措辞不佳,但还可以。

      【讨论】:

        猜你喜欢
        • 2017-09-19
        • 1970-01-01
        • 2016-04-05
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 2015-04-09
        相关资源
        最近更新 更多