V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
r0ck3r
V2EX  ›  git

谁搞清楚过 gitignore 里 xxx, xxx/, xxx/*, xxx/**的区别?

  •  
  •   r0ck3r · Jun 3, 2017 · 4267 views
    This topic created in 3260 days ago, the information mentioned may be changed or developed.

    本身很晕,看了 SO 上有个问题的答案也没明白

    Supplement 1  ·  Jun 3, 2017

    感觉***没啥区别啊,测试目录树:

        a/
        ├── a.txt
        └── b/
            └── b.txt
    

    然后.gitignore里用:

    a/*
    

    然后

    > git add .
    

    会忽略a/b/b.txt,所以,即使是下层目录的文件,也没必要用**

    Supplement 2  ·  Jun 3, 2017

    p.s. Double-star pattern in .gitignore

    After checking fnmatch(3) desctiption and sources I assured that double-star pattern is unsupported by .gitignore. At least directly.

    7 replies    2017-06-03 17:45:19 +08:00
    Chaos11
        2
    Chaos11  
       Jun 3, 2017   ❤️ 1
    * `*` 匹配文件路径中的 0 个或多个字符,但不会匹配路径分隔符,除非路径分隔符出现在末尾
    * `**` 匹配路径中的 0 个或多个目录及其子目录,需要单独出现,即它左右不能有其他东西了。如果出现在末尾,也能匹配文件。
    clino
        3
    clino  
       Jun 3, 2017 via Android
    前面两个有 /的是特指目录吧?
    1010011010
        4
    1010011010  
       Jun 3, 2017
    xxx/ 是不是 包含了 xxx/* 和 xxx/** ?
    Biwood
        5
    Biwood  
       Jun 3, 2017 via Android
    我只知道 xxx 跟 xxx/*是不样的,我没猜错的话,前者会忽略整个文件夹,后者忽略了文件夹的内容但文件夹本身没忽略。

    例如:
    xxx
    !xxx/aaa.txt
    下面那段代码会失效

    这样写就没有问题:
    xxx/*
    !xxx/aaa.txt
    pright
        6
    pright  
       Jun 3, 2017
    单独一个 a 就是当作一个通配符匹配了,路径里面只要包含 a 就会被忽略。

    a/ 是忽略 a 目录和目录下所有内容,我感觉它已经达到了忽略 a 目录下所有内容的目的了,所以后面再加*或者**都没区别了。
    主要是类似 a/*/b 和 a/**/b 这种会有区别,前者只会忽略 a 和 b 中间只有一个目录的情况,后者则是任意目录层级(包括 a/b)都会忽略。
    pright
        7
    pright  
       Jun 3, 2017
    不过 a/*和 a/**在配合!规则时会有点区别,以前被这个坑了几次。主要是因为 a/*其实只是忽略了 a 目录下的内容,a/**则是忽略了下面任意目录层级的内容。如果没有!规则,两者表现会是一样的,因为父目录被忽略,下面的所有内容都会被忽略。

    下面这个例子可以用来说明 a/*和 a/**的区别:
    └── a
    ├── b
    │   ├── c
    │   └── d
    │   └── e
    └── d


    a/*
    !a/b/
    !a/b/c
    结果是
    new file: a/b/c
    new file: a/b/d/e

    a/**
    !a/b/
    !a/b/c
    结果是
    new file: a/b/c
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   922 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 21:17 · PVG 05:17 · LAX 14:17 · JFK 17:17
    ♥ Do have faith in what you're doing.