.gitignore 文件和其他地方的模式

Patterns in the .gitignore file and elsewhere

在下面找到来自 this link:

的两个引用

1) 在DESCRIPTION:

  • Patterns read from the command line for those commands that support them.

2) 在 PATTERN FORMAT 中(重点是我的):

两个引号(上面第二个粗体字)似乎指的是 git ignore 命令(不是文件),在 this answer in SO. The problem that I see here, is that this command is not even mentioned in the Pro Git book.因此,我对上述引述的理解可能是错误的。

我还有一个关于 .gitignore 文件的问题,这是关于 SO 中的 this comment,我在下面重复:

You can put .gitignore anywhere in a git project - if a path starts with /, it will be relative to the location of the .gitignore file; otherwise it will refer recursively to files in the current directory and its descendant directories.

我只是看不出如何从 link given above 中推断出这一点。

好吧,好像有点误会。 pro git 站点上关于 gitignore does refer to the .gitignore file, and not to the command. The command referred to in the other question is not part of git, but of a package called git extras 的文章。

经过更多思考后,我想我开始明白这里有什么令人困惑的地方了。您刚刚引用了(一部分)git 评估忽略模式的顺序。排在最前面的是 patterns from the command line。这些都是处理 git 忽略模式的所有内置和自定义(如在引用的答案中)命令。

gitignore manpage 底部的示例可能最好地解释了第二个引用。我说的是:

$ git status
[...]
# Untracked files:
[...]
# Documentation/foo.html
# Documentation/gitignore.html
# file.o
# lib.a
# src/internal.o
[...]
$ cat .git/info/exclude
# ignore objects and archives, anywhere in the tree.
*.[oa]
$ cat Documentation/.gitignore
# ignore generated html files,
*.html
# except foo.html which is maintained by hand
!foo.html
$ git status
[...]
# Untracked files:
[...]
# Documentation/foo.html
[...]

将git忽略文件放在项目中的任何位置也是完全有效的,请再次参考official documentation on gitignore

Git normally checks multiple sources, with the following order of precedence, from highest to lowest:
- command line
- a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the .gitignore file. A project normally includes such .gitignore files in its repository, containing patterns for files generated as part of the project build.

关于前导斜线的部分在以下段落中描述:

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

换句话说,模式通常被评估为后跟模式的 glob,并将匹配任何子目录中相对于 .gitignore 文件位置的任何路径。但是,如果模式以斜杠开头,则表示模式从相对路径名的开头开始完全匹配。