【Git】.gitignore文件的说明。

官方文档:https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

.gitgnore的作用是用来忽略掉某些文件。粗略翻译下官方文档关于.gitignore 部分(原文在文章最后面)。

.gitgnore是用来忽略掉某些文件的,添加至.gitgnore的文件。将不会被add进Git Respository。

.gitgnore 文件列表只对不在工作区的文件有效,也就是说如果你的文件被 add 到了工作区,那需要先将文件移除工作区(git rm)

关于.gitignore文件有五条规则。

1、空白行或者以#号开始的行将会被忽略,不会生效

2、支持简单的正则匹配

3、你可以使用/来作为一个pattern作为开始来避免递归调用(?反正我不大懂这意思)

4、你可以使用/作为一个pattern的结束符来表示一个具体的目录(这个也不大懂)

5、你可以使用!来将某个文件从忽略列表中剔除。

具体例子如下:

1、注释某行

#  “# 开头的行将会被作为注释不会起到作用,空白行同样不会起作用”

2、忽略所有的java文件

*.java

3、只忽略掉和.gitignore同级的tood文件夹。而不忽略掉res/tood下的tood文件夹

/tood

4、忽略掉tood文件下的所有东西

tood/

5、不忽略hello.java这个文件,即使你开始编写了*.java忽略掉所有java文件的语句,但hello.java这个文件不会被忽略。

!hello.java。

Android项目基本的.gitignore文件的配置

#  忽略bin目录的所有文件
/bin
# 忽略gen目录的所有文件
/gen

 

忽略部分原文摘录如下:

Ignoring Files

Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:

$ cat .gitignore
*.[oa]
*~

The first line tells Git to ignore any files ending in “.o” or “.a” – object and archive files that may be the product of building your code. The second line tells Git to ignore all files whose names end with a tilde (~), which is used by many text editors such as Emacs to mark temporary files. You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. Setting up a .gitignore file before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository.

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.

  • 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:

# no .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 the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

Tip

GitHub maintains a fairly comprehensive list of good .gitignore file examples for dozens of projects and languages at https://github.com/github/gitignore if you want a starting point for your project.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值