.gitignore 遵循什么模式?
What pattern does .gitignore follow?
这是我当前目录的内容。
$ ls
foo.foo
$ ls -a
. .. .bar.foo .foo foo.foo .gitignore
然后我把这个目录变成一个 git 存储库。
$ git init
Initialized empty Git repository in /home/lone/foo/.git/
$ ls -a
. .. .bar.foo .foo foo.foo .git .gitignore
这里是.gitignore
的内容。
$ cat .gitignore
*.foo
我发现 .gitignore
中的模式与 shell 中的相同模式表现不同。在 shell *.foo
中仅匹配非隐藏文件,即不以句点开头的文件名。
$ echo *.foo
foo.foo
但是 .gitignore
中的 *.foo
似乎匹配任何以 .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)
我在哪里可以了解有关 .gitignore 遵循的模式的精确定义的更多信息,以便我可以了解其行为与 shell glob 模式的不同之处以及原因?
.gitignore
仅包含您希望 告诉 git 不跟踪(通配符支持)的模式的条目。
此配置存储在文件夹级别。
在那里你可以指定要忽略的文件。
此文件将以递归方式应用于其中的所有子文件夹。
.gitignore
正在以交换方式收集信息:
System level -(全局)例如,如果您是 unix 用户并且希望忽略所有 *.so
文件对于你所有的项目,你将把它放在全局文件中
Local - 通常会在项目级别申请给定项目的所有内容。
每个 文件夹 - 给定文件夹的特定定义(例如 - 忽略密码文件、日志文件等或您希望忽略的任何其他资源)
还有一个配置 属性 core.excludesfile
允许您设置要使用的忽略文件。
git config --global core.excludesfile ~/.gitignore
您还可以通过在文件前添加 !
来指定 不忽略 的文件,这样它将 不 被忽略(阅读下面有关可以使用的通配符的更多信息)。
要了解语法,您可以阅读它 here。
您可以使用第三部分工具为您生成此文件 gitignore.io。
I couldn't find what in the page explains that *
matches zero or more characters and matches hidden files too
文件中的每一行都可以包含带有以下通配符的任何模式:
?
= Matches zero or one occurrence of the given patterns.
*
= Matches zero or more occurrences of the given patterns.
+
= Matches one or more occurrences of the given patterns.
@
= Matches one of the given patterns.
!
= Matches anything except one of the given patterns.
在您的评论中,您询问了有关 * 模式的信息。
在这里您可以看到 *
忽略了所有文件。 Git 不在乎它是否是隐藏文件。它只是在寻找匹配的模式。
如上所述 git 尝试匹配您在 .gitignore
文件中提供的模式,因此如果您希望忽略内部文件夹中的文件,则必须指定内部文件夹。这是文件夹内容以及如何忽略内部文件的演示。您可以看到内部文件夹 aa
包含文件,但由于忽略文件包含 **/
模式,它将忽略所有内部文件夹和文件。
gitignore
使用“*
”遵循 glob convention:
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
".
OP Lone Learner asks :
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?
因为那是一个shell configuration choice.
类型(使用shopt
, which is specific to bash):
shopt -s dotglob
echo *.foo
.foo foo.foo
那就是使用 dotglob
If set, Bash includes filenames beginning with a '.
' in the results of filename expansion. (for pattern matching)
这是我当前目录的内容。
$ ls
foo.foo
$ ls -a
. .. .bar.foo .foo foo.foo .gitignore
然后我把这个目录变成一个 git 存储库。
$ git init
Initialized empty Git repository in /home/lone/foo/.git/
$ ls -a
. .. .bar.foo .foo foo.foo .git .gitignore
这里是.gitignore
的内容。
$ cat .gitignore
*.foo
我发现 .gitignore
中的模式与 shell 中的相同模式表现不同。在 shell *.foo
中仅匹配非隐藏文件,即不以句点开头的文件名。
$ echo *.foo
foo.foo
但是 .gitignore
中的 *.foo
似乎匹配任何以 .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)
我在哪里可以了解有关 .gitignore 遵循的模式的精确定义的更多信息,以便我可以了解其行为与 shell glob 模式的不同之处以及原因?
.gitignore
仅包含您希望 告诉 git 不跟踪(通配符支持)的模式的条目。
此配置存储在文件夹级别。
在那里你可以指定要忽略的文件。
此文件将以递归方式应用于其中的所有子文件夹。
.gitignore
正在以交换方式收集信息:
System level -(全局)例如,如果您是 unix 用户并且希望忽略所有
*.so
文件对于你所有的项目,你将把它放在全局文件中Local - 通常会在项目级别申请给定项目的所有内容。
每个 文件夹 - 给定文件夹的特定定义(例如 - 忽略密码文件、日志文件等或您希望忽略的任何其他资源)
还有一个配置 属性 core.excludesfile
允许您设置要使用的忽略文件。
git config --global core.excludesfile ~/.gitignore
您还可以通过在文件前添加 !
来指定 不忽略 的文件,这样它将 不 被忽略(阅读下面有关可以使用的通配符的更多信息)。
要了解语法,您可以阅读它 here。
您可以使用第三部分工具为您生成此文件 gitignore.io。
I couldn't find what in the page explains that
*
matches zero or more characters and matches hidden files too
文件中的每一行都可以包含带有以下通配符的任何模式:
?
= Matches zero or one occurrence of the given patterns.
*
= Matches zero or more occurrences of the given patterns.
+
= Matches one or more occurrences of the given patterns.
@
= Matches one of the given patterns.
!
= Matches anything except one of the given patterns.
在您的评论中,您询问了有关 * 模式的信息。
在这里您可以看到 *
忽略了所有文件。 Git 不在乎它是否是隐藏文件。它只是在寻找匹配的模式。
如上所述 git 尝试匹配您在 .gitignore
文件中提供的模式,因此如果您希望忽略内部文件夹中的文件,则必须指定内部文件夹。这是文件夹内容以及如何忽略内部文件的演示。您可以看到内部文件夹 aa
包含文件,但由于忽略文件包含 **/
模式,它将忽略所有内部文件夹和文件。
gitignore
使用“*
”遵循 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
".
OP Lone Learner asks
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?
因为那是一个shell configuration choice.
类型(使用shopt
, which is specific to bash):
shopt -s dotglob
echo *.foo
.foo foo.foo
那就是使用 dotglob
If set, Bash includes filenames beginning with a '
.
' in the results of filename expansion. (for pattern matching)