gpt4 book ai didi

git - .gitignore 目录/通配符意外行为

转载 作者:行者123 更新时间:2023-12-04 03:10:02 28 4
gpt4 key购买 nike

git项目的文件夹结构:

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

文件 .gitignore , file1 , 和 file2都是空的。
我跑 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包含 level1 , level1/ , 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/* 是否 .

最佳答案

根据 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工作。

关于git - .gitignore 目录/通配符意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46001596/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com