2017-09-01 97 views
1
Git项目的

文件夹结构:的.gitignore目录/通配符意外行为

.git/ 
    <some git stuff> 
.gitignore 
level1/ 
    level2/ 
     file1 
     file2 

的文件.gitignorefile1file2都是空的。我运行git add .gitignoregit commit -m "create empty .gitignore"

如果.gitignore不能为空,然后git status输出(如预期)git add -A

new file: level1/level2/file1 
new file: level1/level2/file2 

所以,我git reset开始测试变化的结果向.gitignore(假设我这样做的每一个之间以下更改.gitignore

如果.gitignore包含任一level1level1/level1/*level2,或level2/,然后git add -A其次git status输出(如预期):

modified: .gitignore 

但是,如果.gitignore包含level2/*,然后git add -A其次git status输出:

modified: .gitignore 
new file: level1/level2/file1 
new file: level1/level2/file2 

为什么不level2/*具有相同在这种情况下效果为level2/?另外,虽然level2/*不是做我想要的,**/level2/*确实

回答

1

由于每gitignore documentation

1)如果模式以斜线结尾,那就只能找与目录相匹配。 2)如果模式不包含斜杠/,Git将其视为shell glob模式,并检查与相对于.gitignore文件位置的路径名的匹配(相对于工作树的顶级目录)如果不是从.gitignore文件)。 3)否则,Git会将该模式视为适合fnmatch(3)与FNM_PATHNAME标志一起使用的shell glob:模式中的通配符不会与路径名中的/匹配。例如,“Documentation/*。html”与“Documentation/git.html”匹配,但不匹配“Documentation/ppc/ppc.html”或“tools/perf/Documentation/perf.html”。

该模式level2/*属于第三种情况。由于在项目的根目录下没有目录level2,因此git不会忽略level1/level2/中的文件。

这就是为什么**/level2/*确实有效,以及为什么level2/level2有效。