gitignore规则

简介: 笔记

在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


目录
相关文章
|
6天前
|
缓存 开发工具 git
git过滤不需要的修改提交内容.gitignore的使用
git过滤不需要的修改提交内容.gitignore的使用
5 1
|
开发工具 git 缓存
Git忽略规则.gitignore不生效
在项目开发过程中个,一般都会添加 .gitignore 文件,规则很简单,但有时会发现,规则不生效。 原因是 .gitignore 只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
51908 4
|
9月前
|
XML 缓存 开发工具
【Git】Git忽略提交的规则(.gitignore配置总结)
【Git】Git忽略提交的规则(.gitignore配置总结)
159 0
|
10月前
|
开发工具 git
gitignore的匹配规则
gitignore的匹配规则
|
11月前
|
开发工具 git
Git的.gitignore文件、标签管理以及给命令起别名
Git的.gitignore文件、标签管理以及给命令起别名
|
开发工具 git
git 排除已经提交的文件目录
如果你已经把一个文件夹提交到了 Git 仓库中,但是后来发现这个文件夹不应该被提交,可以按照以下步骤排除已提交的文件夹: 在文件夹的根目录下创建一个名为 .gitignore 的文件。 编辑 .gitignore 文件,添加需要排除的文件夹的名称,以及其他需要排除的文件或文件类型,每个名称占一行。 执行以下命令,将 .gitignore 文件提交到 Git 仓库中:
478 0
|
Java 开发工具 git
.gitignore忽略文件如何配置
.gitignore忽略文件如何配置
|
前端开发 开发工具 git
.gitignore前端Git忽略提交的规则
.gitignore前端Git忽略提交的规则
330 0
|
Python
gitignore 规则
gitignore 规则
70 0
|
开发工具 git
.gitignore的用法详解
在 .gitingore 文件中,每一行指定一个忽略规则,Git 检查忽略规则的时候有多个来源,
186 0