前导星号"**/"是否为.gitignore路径匹配语法中是否多余? [英] Are leading asterisks "**/" redundant in .gitignore path matching syntax?
问题描述
有没有没有星号的用法不能替代的用法?
Are there any usages that can't be replaced by equivalents without asterisks?
Two consecutive asterisks ("**") in patterns matched against full
pathname may have special meaning:
A leading "**" followed by a slash means match in all directories.
For example, "**/foo" matches file or directory "foo" anywhere,
the same as pattern "foo". "**/foo/bar" matches file or directory
"bar" anywhere that is directly under directory "foo".
A trailing "/**" matches everything inside. For example, "abc/**"
matches all files inside directory "abc", relative to the location
of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches
zero or more directories. For example, "a/**/b" matches "a/b",
"a/x/b", "a/x/y/b" and so on.
Other consecutive asterisks are considered invalid.
https://git-scm.com/docs/gitignore#_pattern_format
让我指出,我只问有关前导星号/斜杠冗余的问题.由于似乎任何**/foo/bar
都可以用简单的foo/bar
Let me point out, that i'm asking only about leading asterisks/slash redundancy. Since it seems any **/foo/bar
can be replaced by simple foo/bar
例如,我的.gitignore
文件中包含以下内容:
For example, i have the following in my .gitignore
file:
# NuGet Packages
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
我想知道为什么他们不能简单地写:
And i'm wondering, why they couldn't simply write:
# NuGet Packages
# The packages folder can be ignored because of Package Restore
packages/*
# except build/, which is used as an MSBuild target.
!packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!packages/repositories.config
https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
推荐答案
不,仅当其余模式除尾部以外不包含斜杠时,前导**/
才过时.
No, the leading **/
is only obsolete if the remaining pattern does not contain a slash except for a trailing one.
https://git-scm.com/docs/gitignore 上的文档是我认为这有点误导
The documentation at https://git-scm.com/docs/gitignore is a bit misleading in my opinion, as it states
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).
但实际上仅与文件或目录的名称匹配,而不与整个路径匹配.
but actually matches only against the name of a file or directory, not the whole path.
所以**/packages/build
在任何深度都匹配packages/build
.
packages/build
由于包含斜杠,因此实际上与/packages/build
相同,并且仅在与忽略文件相同的级别上匹配packages/build
.
So **/packages/build
matches packages/build
in any depth.
packages/build
as it contains a slash is effectively the same as /packages/build
and only matches packages/build
on the same level as the ignore file.
另一方面,build
在任何深度都匹配build
,因此实际上与**/build
相同.
而/build
仅与build
在与忽略文件相同的级别上匹配.
On the other hand build
matches build
in any depth so is effectively the same as **/build
.
Whereas /build
only matches build
on the same level as the ignore file.
这始终取决于模式(如果存在,则删除尾部斜杠之后)是否包含任何斜杠.
It always depends on whether the pattern (after removing a trailing slash if present) contains any slashes or not.
这篇关于前导星号"**/"是否为.gitignore路径匹配语法中是否多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!