git官网上关于.gitignore的部分的简要翻译

 

简略的翻译了git官网上关于.gitignore的部分,点击查看原文html

§NAME

gitignore - Specifies intentionally untracked files to ignoregit

§SYNOPSIS

$HOME/.config/git/ignore, $GIT_DIR/info/exclude, .gitignoreshell

§DESCRIPTION

略,点击查看原文less

§PATTERN FORMAT

.gitignore文件里表达式的一些用法:ide

  • 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.ui

    以“#”开头的是注释行。“\”放在表达式前面用来转义this

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

    行尾的空格会被忽略,除非在空格前加“\”才会生效

  • 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”.

    “!”前缀选项:否认模式,即已经被前面ignore表达式匹配后忽略的文件,从新被包含进git。

    可是,若是此文件的父目录已经被忽略了,那么此文件就不能再从新包含进git,由于处于性能的考虑。git并不会去检索被忽略的目录,因此.gitignore里面的表达式并不会去匹配这些目录下的文件;

    放一个“\”在“!”前面能够转义“!”为一个简单的字符(即失去了否认模式的功能)

    \!file!.txt 匹配文件“!file!.txt”,第一个“!”位于行首,被转义为简单的字符,第二个“!”就是个简单的字符,由于“!”只有放在行首才拥有否认模式的效果

  • If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).

    表达式以“/”结尾的话,只会匹配目录;

    例如“foo/”将只会匹配foo目录和目录里面全部东西,但不会匹配foo文件和foot软链接

  • If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

    若是表达式里面没有“/”,那么git会把表达式当作shell表达式去匹配和.gitignore同级目录下的文件(对于那些不是在.gitignore文件里的表达式,会相对于工做目录去查找),git命令也能够设置ignore表达式

  • Otherwise, 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”.

    通配符“*”不会匹配路径字符串中的“/”,

    "Documentation/*.html" 匹配 "Documentation/git.html” 

    但不匹配"Documentation/ppc/ppc.html" 或者 "tools/perf/Documentation/perf.html”.

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

    “/”开头的只匹配当前目录下的东西;

    “/*.c”匹配“cat-file.c”,但不匹配“hello/sha.c”

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

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

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

  • Other consecutive asterisks are considered invalid.

    连续的两个星号“**”能匹配完整的路径,但会有些特殊的意思:

    以“**”开头,紧跟着一个“/”会在全部目录下去匹配;例如“**/foo”会匹配任何 叫foo的目录或文件,效果等同于表达式“foo”;"**/foo/bar" 会匹配任何位于foo目录下,名为”bar”的目录或文件 (即foo目录能够再任何地方)。

     

    “/**”会匹配目录下的任何东西;例如“abc/**”,会匹配“abc”目录下的全部文件,不限目录深度,可是“abc”目录的位置必须和“.gitignore”同级。(通过测试,凡是此种“name/**”格式,即以“/**”或者“/*”结尾的,都会限制只在.gitignore同级目录下搜索name文件夹)

    “/**/”匹配0层或多层目录,例如,"a/**/b" 匹配 "a/b", "a/x/b", "a/x/y/b

    其余形式的“**”被认为是无效的

§NOTES

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked.

To stop tracking a file that is currently tracked, use git rm --cached.

相关文章
相关标签/搜索