0%

gitignore优先级规则

  • 在 .gitignore 文件中,可以设置多条规则来匹配不同的文件或文件夹。如果同一个文件被多条规则命中,那么 Git 会根据这些规则的优先级来决定是否忽略该文件。
  • 优先级规则如下:
    • 优先级最高的是明确指定的文件或文件夹路径。例如,/file.txt 会明确匹配根目录下的 file.txt 文件。
    • 如果一个文件同时被多条模式匹配,那么 Git 会根据 .gitignore 文件中规则的顺序来决定优先级。在 .gitignore 文件中,越靠近底部的规则优先级越高。
    • 如果一个文件被多条模式匹配,但其中有一条是以 “!” 开头的(表示不忽略该文件),那么该文件将不会被忽略。例如:
      1
      2
      3
      4
      # .gitignore 文件内容
      *.txt
      !important.txt
      important.txt
      • 在这个例子中,尽管 important.txt 文件同时被 *.txt 和 important.txt 规则匹配,但是 !important.txt 规则会将其排除在外,所以该文件不会被忽略。
  • 参考 https://git-scm.com/docs/gitignore

A blank line matches no files, so it can serve as a separator for readability.
空行不匹配任何文件,因此它可以用作可读性的分隔符。

A line starting with # serves as a comment. Put a backslash (“") in front of the first hash for patterns that begin with a hash.
以 # 开头的行用作注释。对于以哈希开头的模式,在第一个哈希前面放置一个反斜杠 (“\”)。

Trailing spaces are ignored unless they are quoted with backslash (“").
尾随空格将被忽略,除非它们用反斜杠 (“\”) 括起来。

An optional prefix “!” which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash (“") in front of the first “!” for patterns that begin with a literal “!”, for example, “!important!.txt”.
一个可选的前缀 “!”,用于否定模式;先前模式排除的任何匹配文件都将再次包含在内。如果排除了文件的父目录,则无法重新包含该文件。出于性能原因,Git 不会列出排除的目录,因此无论它们在何处定义,包含文件上的任何模式都不起作用。对于以文本 “!” 开头的模式,在第一个 “!” 前面加上反斜杠 (“\”),例如 “\!important!”。txt“的

The slash “/“ is used as the directory separator. Separators may occur at the beginning, middle or end of the .gitignore search pattern.
斜杠 “/” 用作目录分隔符。分隔符可能出现在 .gitignore 搜索模式的开头、中间或结尾。

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.
如果模式的开头或中间(或两者)有分隔符,则该模式是相对于特定 .gitignore 文件本身的目录级别。否则,模式也可能在 .gitignore 级别以下的任何级别匹配。

If there is a separator at the end of the pattern then the pattern will only match directories, otherwise the pattern can match both files and directories.
如果模式末尾有分隔符,则该模式将仅匹配目录,否则该模式可以同时匹配文件和目录。

For example, a pattern doc/frotz/ matches doc/frotz directory, but not a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a directory (all paths are relative from the .gitignore file).
例如,模式 doc/frotz/ 与 doc/frotz 目录匹配,但与 a/doc/frotz 目录不匹配;但是 frotz/ 匹配 frotz a/frotz 是一个目录(所有路径都是相对于 .gitignore 文件的)。

An asterisk “*“ matches anything except a slash. The character “?” matches any one character except “/“. The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.
星号 “*” 匹配除斜杠之外的任何内容。字符 “?” 匹配除 “/” 之外的任何一个字符。范围表示法(例如 [a-zA-Z]) 可用于匹配范围内的一个字符。有关更详细的描述,请参见 fnmatch(3) 和 FNM_PATHNAME 标志。

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”.
前导 “**” 后跟斜杠表示在所有目录中匹配。例如,“**/foo” 匹配任何位置的文件或目录 “foo”,与模式 “foo” 相同。“**/foo/bar” 匹配目录 “foo” 下任何位置的文件或目录 “bar”。

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.
尾部的 “/**” 匹配里面的所有内容。例如,“abc/**” 匹配目录 “abc” 内的所有文件,相对于 .gitignore 文件的位置,具有无限的深度。

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.
一个斜杠后跟两个连续的星号,然后一个斜杠匹配零个或多个目录。例如,“a/**/b” 匹配 “a/b”、“a/x/b”、“a/x/y/b” 等。

Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.
其他连续的星号被视为常规星号,并将根据前面的规则进行匹配。