在Pro Git一书中有如下描述
The rules for the patterns you can put in the .gitignore file are as follows:
• Blank lines or lines starting with # are ignored.
• Standard glob patterns work, and will be applied recursively throughout the entire working tree.
• You can start patterns with a forward slash (/) to avoid recursivity.
• You can end patterns with a forward slash (/) to specify a directory.
• You can negate a pattern by starting it with an exclamation point (!)
Glob patterns are like simplified regular expressions that shells use.
- An asterisk (*) matches zero or more characters;
- [abc] matches any character inside the brackets (in this case a, b, or c);
- a question mark (?) matches a single character;
- and brackets enclosing characters separated by a hyphen ([0-9]) matches any character between them (in this case 0 through 9).
- You can also use two asterisks to match nested directories; a/**/z would match a/z, a/b/z, a/b/c/z, and so on.
Here is another example .gitignore file:
# ignore all .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf
翻译
• 空行或者以#开头的行是不生效的
• 标准的glob工作模式,遍历整个工作目录
• 以/开头可以避免递归匹配
• 以/结尾指定匹配目录
• !声明,为不匹配指定文件或者目录,即不被忽略
Glob模式类似于shell使用的简化正则表达式:
- 星号(*)可以匹配0个或者多个字符
- [abc]可以匹配中括号中的任意一个字符
- 问号(?)匹配任意一个字符
- [0-9]可以匹配0到9之间的任意一个数字
- 使用两个星号可以匹配多级目录,例如a/**/z,可以匹配a/z,a/b/z,a/b/c/z等等
下面是一个.gitignore的例子:
# 忽略所有的.a格式的文件 *.a # 但是要追踪lib.a,即lib.a不会被忽略,尽管上面已经忽略了所有的.a文件,这就是!的作用 !lib.a # 只忽略当前文件夹下面的TODO文件,而不会递归子文件夹 /TODO #忽略所有build文件夹下面的文件和文件夹 build/ #忽略doc文件夹下面的.txt文件,但是不会递归子文件夹下面的.txt doc/*.txt # 递归忽略doc文件夹下面的所有的.pdf文件 doc/**/*.pdf