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.

相關文章
相關標籤/搜索