.gitignore文件内容格式介绍

.gitignore文件内容格式介绍

引言

我们在使用github或者gitee进行代码管理的时候,经常要接触git,很多时候我们希望项目中的一些文件不被纳入版本控制。比如我一个java工程,每次改改代码编译之后都会生成class文件,但是我不希望对我的class文件进行版本控制,希望git可以把class文件ignore掉。
此时,就需要我们的.gitignore文件出场了。这里我就简单介绍一下.gitignore文件内容的格式。

中文

  • 空行不匹配任何文件,可以用来对内容分割增强阅读性

  • #开头表示注释。如果格式需要#开头,那么需要转义一下:前面加上反斜杠("\")

  • 末尾的空格会被忽略,除非被反斜杠引用

  • "!"前缀可以使他后面的模式变得无效。他可以用于把之前被忽略的文件再次包含进来。但是出于性能考虑被父目录所忽略的文件,这里不会再次被包含进来。如果被忽略的文件以!开头,那么需要转义一下。

  • 以斜杠结尾的模式,匹配文件夹。例如foo/会匹配上foo文件夹,但是不会匹配普通的文件foo或者符号文件。

  • 如果一个模式中不包含斜杠,那么他会被当做shell的glob模式进行匹配,匹配的位置为当前.gitignore所在的文件夹,如果没有.gitignore那就是工作区的顶层目录

  • 除此之外,Git会使用一下的shell glob模式: “*” 可以匹配除了 “/“以外的任何东西,”?” 可以匹配除了 "/“以外的任意字符,”[]"可以匹配一个在特定范围内的字符

  • 开头使用的反斜杠匹配路径的开始位置。例如:"/*.c"可以匹配"cat-file.c"文件,但是不能匹配"mozilla-sha1/sha1.c"文件

连续的两个星号具有特殊的含义:

  • **/“开头表示匹配所有目录。例如: “**/foo” 匹配所有所有目录下名称为foo的文件和文件夹,”**/foo/bar” 匹配所有foo文件夹下名称为bar的文件和文件夹
  • "/**"结尾表示匹配在他下面的所有东西。例如: "abc/**"匹配所有在abc文件夹下的文件,当然这个abc的路径是相对于 .gitignore 文件所在的文件夹。
  • "/**/"匹配零到多个文件夹。例如: “a/**/b“可以匹配”a/b”, “a/x/b”, "a/x/y/b"等等。
  • 其他连续的*将被认为是普通的星号,按照上述的规则进行匹配。

英文

  • 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: “*” matches anything except “/”, “?” matches any one character except “/” and “[]” matches one character in a selected range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.
  • 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.
  • 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 regular asterisks and will match according to the previous rules.

声明

英文内容来自git文档,中文内容自己翻译。希望各位小伙伴有问题留言讨论!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值