.gitignore遵循什么模式? [英] What pattern does .gitignore follow?
问题描述
$ ls
foo.foo
$ ls -a
。 .. .bar.foo .foo foo.foo .gitignore
然后我把这个目录变成git
$ git init
在/home/lone/foo/.git/ $ b中初始化了空的Git仓库$ b $ ls -a
。 .. .bar.foo .foo foo.foo .git .gitignore
这里是< code .gitignore 。
$ cat .gitignore
* .foo
我发现 .gitignore
行为与shell中的相同模式有所不同。在shell *。foo
中只能匹配非隐藏文件,即不以句点开头的文件名。
$ echo * .foo
foo.foo
但是 *。foo
在 .gitignore
中似乎匹配任何以隐藏或非隐藏结尾的文件, code> .foo 。
$ git status
分支master
初始提交
未经记录的文件:
(使用git add< file> ...来包含将要提交的内容)
.gitignore
没有添加任何内容,但未跟踪的文件存在(使用git add跟踪)
在哪里可以了解更多关于.gitignore所遵循的模式的精确定义,以便我能够理解其行为与shell glob模式的区别和原因?
$ *
'遵循 glob convention :
Git将模式视为适合消费的shell glob。
fnmatch
(3) 与FNM_PATHNAME
标记 :模式中的通配符不匹配例如,Documentation / *。html
/
在路径名中。 / code>matchesDocumentation / git.html
但不是Documentation / ppc / ppc.html
或tools / perf / Documentation / perf.html
。
OP Lone Learner 要求在评论中:
shell是否也使用
fnmatch
(3) 来处理glob模式?
在这种情况下,为什么*
不匹配之前的零个字符。 (即隐藏文件),但gitignore呢?
因为这是一个 shell配置选项。
类型(使用 shopt
,这是
shopt - s dotglob
echo * .foo
.foo foo.foo
即使用dotglob
如果设置,Bash包含以'
。
'开头的文件名 文件名扩展的结果 STRONG>。 (用于模式匹配)
Here is the content of my current directory.
$ ls
foo.foo
$ ls -a
. .. .bar.foo .foo foo.foo .gitignore
Then I turn this directory into a git repository.
$ git init
Initialized empty Git repository in /home/lone/foo/.git/
$ ls -a
. .. .bar.foo .foo foo.foo .git .gitignore
Here is the content of .gitignore
.
$ cat .gitignore
*.foo
I see that the pattern in .gitignore
behaves differently from the same pattern in shell. In the shell *.foo
matches only non-hidden files, i.e. filenames that do not begin with a period.
$ echo *.foo
foo.foo
But *.foo
in .gitignore
seems to match any files, hidden or non-hidden, that ends with .foo
.
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
Where can I learn more about the precise definition of the patterns that .gitignore follows, so that I can understand where and why its behaviour differs from that of the shell glob patterns?
gitignore
use of '*
' follows the glob convention:
Git treats the pattern as a shell glob suitable for consumption by
fnmatch
(3) with theFNM_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
".
The OP Lone Learner asks in the comments:
Does the shell also use
fnmatch
(3) to process glob patterns?
In that case why does*
not match zero characters before. (i.e. hidden files) but gitignore does?
Because that is a shell configuration choice.
Type (using shopt
, which is specific to bash):
shopt -s dotglob
echo *.foo
.foo foo.foo
That is using dotglob
If set, Bash includes filenames beginning with a '
.
' in the results of filename expansion. (for pattern matching)
这篇关于.gitignore遵循什么模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!