gitignore

在 Git 项目内建立 .gitignore,写入需要忽略的文件,或者在 ~/.gitignore_global 写入文件进行全局忽略 然后对git进行全局设置,让 git 忽略 .gitignore_global 中的所有文件:

git config --global core.excludesfile ~/.gitignore_global
 
# or in config file
[user]
name = xxx
email = xxx@mail.com
 
[core]
excludesfile = ~/.gitignore_global

这样就不用每个 git 目录都设置忽略 .DS_Store 等文件了

注释与转义字符

格式 说明 例子 匹配
以 # 开头 注释 # comment
* 匹配任意字符,不包括/ /d/*.html /d/f.html,但不匹配/d/e/f.html
? 匹配一个字符,不包括/ /d/?.html /d/f.html,但不匹配/d/ef.html

路径规则

格式 说明 例子 匹配
以 / 开头 当前 .gitignore 所在目录 /*.html /f.html,不匹配d/f.html
不指定路径 文件匹配规则 *.html /下所有html文件
不指定路径 文件匹配规则 a.html 所有a.html
路径匹配 路径 /b/a.html 根目录下/b/a.html
路径匹配 路径 b/a.html 同上,不会匹配c/b/a.html

多层目录

格式 说明 例子 匹配
以 **/ 开头 配所有的文件夹 **/test.md 所有的test.md文件
以 /** 结尾 匹配文件夹内所有内容 a/** 文件夹a中所有
** 被 / 夹住 表示匹配0或者多层文件夹  a/**/b a/b 、a/x/b、a/x/y/b 

取反

  • 前缀 ! 表示如果前面匹配到被忽略,则重新添加回来

  • 如果匹配到的父文件夹还是忽略状态,该文件还是保持忽略状态

  • 如果路径名第一个字符为 ! ,则需要在前面增加 \ 进行转义