代码之家  ›  专栏  ›  技术社区  ›  Timmmm

Git正在忽略。父目录中的gitignore文件

  •  5
  • Timmmm  · 技术社区  · 7 年前

    我有一个这样的结构:

    projects/
        .gitignore
        foo/
            some_file
        bar/
            another_file
    

    foo , bar , projects 是git项目。在里面 .gitignore 我有 * (事实上我试了很多东西),但是当我跑的时候 git status 在…内 projects/foo 它仍然告诉我要添加 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)
    
    3 回复  |  直到 3 年前
        1
  •  4
  •   iBug    7 年前

    Git仅在中使用规则 .gitignore 文件 在currect存储库中 . 您可能需要符号链接或硬链接 .gitignore 如果要将文件应用于所有子存储库,请将其从父目录复制到所有子目录。

        2
  •  1
  •   VonC    3 年前

    Git 2.33(2021第3季度)澄清了 .gitignore 检测:

    看见 commit e041706 (2021 7月6日) Andrew Berry ( deviantintegral ) .
    (合并人 Junio C Hamano -- gitster -- 在里面 commit bb3a55f ,2021 7月22日)

    docs : .gitignore解析位于repo的顶部

    签字人:Andrew Berry

    当前文档的内容如下 .gitignore 文件将在每个父目录中解析,直到它们到达存储库边界。
    这澄清了当前的行为。

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

    gitignore 现在包括在其 man page :

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

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

    由于您在中初始化了嵌套Git存储库 /private/tmp/projects/foo/.git/ 这个 .gitignore 在里面 projects/ 将被忽略。

        3
  •  0
  •   Timmmm    7 年前

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

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

    措辞拙劣但还行。