Git工具 .ignore编写规范

前言

Git是目前开发中使用最多、功能最强的代码版本管理工具,能够高效地使用它是程序开发人员非常重要的一个课题,今天这里主要总结下.gitignore文件的编写方法,之前并没有太关注这一块,现在补回来。在我看来,每次提交代码的时候要取消勾选大量的文件实在是一种噩梦。

 

文件语法

为了准确性和权威性,我直接将Git官网上对gitigore相关的语法复制过来了,方便大家对照查阅。

A blank line matches no files, so it can serve as a separator for readability.

1.空行不会匹配文件,它可以看作只是阅读的分隔符;

A line starting with # serves as a comment. Put a backslash ("\") in front of the first hash for patterns that begin with a hash.

2.以#开头的行是注释行。对于以散列开头的模式,在第一个散列前面放置一个反斜杠(“\”)。

Trailing spaces are ignored unless they are quoted with backslash ("\").

3.尾随空格将被忽略,除非它们用反斜杠 ("\") 引起来。

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

 

The slash / is used as the directory separator. Separators may occur at the beginning, middle or end of the .gitignore search pattern.

5.斜杠 / 用作目录分隔符,分隔符可能出现在 .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.

6.如果模式的开头或中间(或两者)有分隔符,则模式与特定 .gitignore 文件本身的目录级别相关。 否则,该模式也可能在 .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. 

7.“*”匹配除“/”以外的任何内容。  ”?” 匹配除“/”之外的任何一个字符。 范围符号,例如 [a-zA-Z],可用于匹配范围内的字符之一。 

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

8.与完整路径名匹配的模式中的两个连续星号(“**”)可能具有特殊含义:

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

9.前导“**”后跟"/"表示在所有目录中匹配。 例如,“**/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.

10.尾随的“/**”匹配里面的所有内容。 例如,“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.

11.一个斜杠后跟两个连续的星号则匹配零个或多个目录。 例如,“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.

12.其他连续的星号被认为是常规星号,将按照前面的规则进行匹配。

.gitignore文件作用域

gitignore文件作用域有两种,分别是当前项目和所有项目。

当前项目

很好理解,将.gitignore文件新建在某个项目的根目录下,此时这个.gitignore文件只对当前项目生效,作用域只限于当前项目;此.gitignore文件可以提交到远端实现多人共享一套屏蔽规则;

所有项目

除了可以在某个项目中定义 .gitignore 文件外,还可以设置全局的.gitignore文件来管理所有项目的git行为。这种方式在不同的项目开发者之间是不共享的,是属于项目之上Git应用级别的行为。这种方式也需要创建相应的 .gitignore 文件,可以放在任意位置。然后再使用以下命令配置Git:

git config --global core.excludesfile ~/.gitignore

注意事项

.gitignore文件对于已经本地缓存的文件不生效,可以通过以下方式清空本地git缓存后再重新加人使编写的.gitignore文件生效:

git  rm  -r  --cached  .    #清除本地缓存,注意有个“.”在后面,别落了

git  add  .   #重新加入本地缓存,同样注意后面有个 “.”

posted @ 2023-04-27 21:45  HexThinking  阅读(386)  评论(0编辑  收藏  举报