.gitignore directory/wildcard 意外行为

.gitignore directory/wildcard unexpected behaviour

git 项目的文件夹结构:

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

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

如果 .gitignore 为空,git add -A 后跟 git status 输出(如预期):

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

因此,我 git reset 开始测试 .gitignore 更改的结果(假设我在 .gitignore 的以下每个更改之间执行此操作)

如果 .gitignore 包含 level1level1/level1/*level2level2/,则 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/* .

根据 gitignore documentation:

1) If the pattern ends with a slash, it would only find a match with a directory.

2) If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

3) Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

模式level2/*属于第三种情况。由于项目根目录下没有目录level2,所以git不会忽略level1/level2/.

中的文件

这就是 **/level2/* 有效的原因,也是 level2/level2 有效的原因。