gitignore

.gitignore 一些规则

  • 所有空行或者以 # 开头的行都会被 Git 忽略
  • 可以使用 glob 模式匹配
  • 匹配模式最后跟反斜杠 / 说明要忽略的是目录
  • / 开头防止递归
  • 要忽略执行模式意外的文件或目录,可以在模式前加上惊叹号 ! 取反

glob

所谓 glob 模式是指 shell 使用的简化的正则表达式。

  • 星号 * 匹配零或多个任意字符。
  • 星号 ** 匹配任意中间目录,比如:a/**/z 可以匹配 a/z, a/b/z 或 a/b/c/z 等。
  • [abc] 匹配任何一个列在方括号中的字符。
  • ? 只匹配一个任意字符。
  • 如果方括号中使用短划线分割两个字符,表示所有在这两个字符范围内的都可以匹配 [0-9]

git 的所有命令后面都可以使用 glob 模式,比如 git rm log/\*.log 注意到 * 之前的反斜杠 \, 因为 Git 有它自己的文件模式扩展匹配方式,所以我们不用 shell 来帮忙展开。 此命令删除 log/ 目录下扩展名为 .log 的所有文件。

类似的还有 git rm \*~ 命令为删除以 ~ 结尾的所有文件。

简单的例子

# 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

Rails Project 的例子

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile '~/.gitignore_global'

# OS generated files
# ------------------
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
.tags*
.gemtags
# Icon?
ehthumbs.db
Thumbs.db

# Ignore all logfiles and tempfiles.
/log
/tmp
!/log/.keep
!/tmp/.keep
.idea/
/public/uploads
=======
*.rbc
capybara-*.html
.rspec
/public/system
/coverage/
/spec/tmp
**.orig
rerun.txt
pickle-email-*.html

# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb
onfig/master.key
# Only include if you have production secrets in this file, which is no longer a Rails default
# config/secrets.yml

# some custom config.
config/database.yml
config/oneapm.yml
config/application.yml
config/keys/rsa_private_key.pem
config/keys/pingpp_rsa_public_key.pem
config/puma.rb

# dotenv
# TODO Comment out this rule if environment variables can be committed
.env

## Environment normalisation:
# Ignore bundler config.
/.bundle
/vendor/bundle
/app/assets/javascripts/i18n/*
/vendor/assets/bower


# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
/db/pictures

# these should all be checked in to normalise the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# if using bower-rails ignore default bower_components path bower.json files
/vendor/assets/bower_components
*.bowerrc
bower.json

# Ignore pow environment settings
.powenv

# Ignore Byebug command history file.
.byebug_history

# Ignore node_modules
node_modules/

# Editor
*.sublime-workspace

相关链接

  • https://github.com/github/gitignore

如果觉得我的文章对您有用,请在支付宝公益平台找个项目捐点钱。 @Victor Apr 15, 2018

奉献爱心