当前位置:  开发笔记 > 开发工具 > 正文

.gitignore路径匹配语法中的前导星号“ ** /”是否多余?

如何解决《.gitignore路径匹配语法中的前导星号“**/”是否多余?》经验,为你挑选了1个好方法。

有没有用星号不能替代的用法?

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

例如,我的.gitignore文件中包含以下内容:

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.

我想知道为什么他们不能简单地写:

# 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



1> Vampire..:

不可以,**/仅当其余模式除尾部斜杠以外不包含斜杠时,前导符才作废。

我认为https://git-scm.com/docs/gitignore上的文档有点误导

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).

但实际上仅与文件或目录的名称匹配,而不与整个路径匹配。

因此可以进行任何深度的**/packages/build匹配packages/build
packages/build因为它包含一个斜杠实际上与忽略文件相同,/packages/build并且仅packages/build在与忽略文件相同的级别上匹配。

另一方面,在任何深度都build匹配build,因此实际上与相同**/build
/build仅匹配build在与忽略文件相同的级别上进行。

它始终取决于模式(在删除尾部斜线(如果存在)之后)是否包含任何斜线。

推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有