hello/
忽略文件夹结构中任何位置名为“hello”的所有文件夹
hello/*
仅忽略顶级文件夹“hello”。
这是为什么?请指出http://git-scm.com/docs/gitignore中的一段话这解释了这种行为。
最佳答案
这可能看起来很奇怪,但这允许我们以多种方式匹配目录:任何地方的文件或目录,任何地方的目录,或顶级目录。这种多功能性非常有用,可以防止 .gitignore
困惑。
(仅供引用,如果你有 Git 1.8.2+,你可以使用 git check-ignore
来帮助调试它。)
如果你输入 foo
,它将匹配所有名为 foo
的文件和目录。
如果你输入 foo/
,它只会匹配名为 foo
的目录。
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).
如果添加 *
,如 foo/*
,它会被视为文件 glob(相对于 .gitignore
)。
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".
因此对于foo/*
,Git会忽略顶级目录foo
中的所有文件和目录。它会忽略 foo/dir
、foo/file.txt
等(从技术上讲,这不会忽略 foo
本身,但它会忽略它的子项。但是,由于 Git 不跟踪目录,因此它具有相同的效果。)
仅供引用,foo/**
会有相同的行为。
我的建议:
如果你想忽略顶层的目录 foo
,IMO 最清楚的是使用这个规则:
A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
所以你可以写/foo/
,它会忽略顶层目录foo
,但不会忽略其他地方。
关于gitignore:了解 "hello/"与 "hello/*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23857207/