海滨擎蟹

git 忽略文件规则(gitignore)

忽略规则是对文件名有效的

A: 空行或#号开始的行,会被忽略;

B: 可以使用通配符:

C: 路径分隔符"/";

如果"/"后面的名称是个目录,则该目录以及该目录下的所有文件都会被忽略;

如果"/"后面的名称是个文件,则该文件不会被忽略;

例如: /name
如果name是个目录,则目录name和name下的所有文件都会被忽略;
如果name是个文件,则该文件不会被忽略;

D: .gitignore文件也可以忽略自己,只要把自己的名字写进来即可;

E: 一条(行)忽略规则只对某一个目录下的文件有效,而对该目录下的子目录中的文件无效;

F: 一条(行)忽略规则也可以只对单个文件有效(忽略单个指定的文件);

例如:
*.a #忽略所有以.a为后缀的文件;
!lib.a #不忽略文件lib.a;
/TODO #只忽略此目录下TODO文件,子目录的TODO不被忽略;
build/ #忽略当前目录之下的 build 目录下的所有文件;
build #忽略 任何目录 build文件;
/build #忽略build根目录下的build文件;
/build/ #忽略build根目录下的build目录下的所有文件;
doc/*.txt #只忽略doc/下所有的txt文件,但是不忽略doc/subdir/下的txt文件;

可以使用标准的 glob 模式匹配

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。

星号(*)匹配零个或多个任意字符;

[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);

问号(?)只匹配一个任意字符;

如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。

使用两个星号() 表示匹配任意中间目录,比如a/*/z 可以匹配 a/z, a/b/z 或 a/b/c/z等。

/path/to/ 和 path/to/ 的区别

前者表示 忽略 根路径 path/to 下的所有文件,后者表示 忽略 任何目录 下 path/to 文件夹下的所有文件
末尾加 / 表示文件夹,代表忽略文件夹下的所有文件。不加 / 表示文件(也可以表示文件夹),代表忽略该文件。

规则:!.gitignore
跟踪 .gitignore 文件
说明:忽略全部内容<如果存在一行 * >,但是不忽略 .gitignore 文件

如果要忽略的文件已被git管理,需要先移除,命令如下: -r:递归

If you already have a file checked in, and you want to ignore it, 
Git will not ignore the file if you add a rule later. 
In those cases, you must untrack the file first, by running the following command in your terminal:
git rm -r --cached  WebRoot/WEB-INF/classes/**/*

在线生成 .gitignore

gitignore.io - Create Useful .gitignore Files For You Project

扩展文章摘录

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

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

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

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

A leading slash matches the beginning of the pathname. 
For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.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.
"abc/*.*" : relative to the location of the .gitignore file only

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.

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »