github/gitignore · Rails.gitignore

Ruby on Rails .gitignore 模板详解

说明 Rails.gitignore 忽略 log、tmp、storage、开发用 SQLite、master.key、预编译资源和 node_modules 的原因,以及 .keep 否定模式的结构。

在生成器中打开 与 Ruby, Node, macOS 一起打开

Rails.gitignore 拦截 Rails 应用运行期间产生的文件和机密信息。主要对象是日志、临时文件、上传文件等服务器运行时积累的数据,以及资源管道生成的产物。

/log/*!/log/.keep 这样只排除目录中的内容、保留空的占位文件的模式出现了三次。git 不保存空目录,因此要保留目录结构就需要这种方式。

逐条规则详解

模式忽略什么,为什么忽略
/log/*/tmp/*/storage/*!/log/.keep!/tmp/.keep!/storage/.keep日志、临时与存储目录存放运行日志、缓存与 pid 文件、Active Storage 本地上传的目录。排除其中内容,但恢复 .keep 文件,使目录保留在仓库中。如果像 /log/ 那样排除目录本身,就无法恢复 .keep
config/master.keyconfig/initializers/secret_token.rb.env.env*.local密钥config/master.key 是打开加密的 credentials.yml.enc 的钥匙。一旦泄露,所有凭据都会暴露,因此绝对不要提交。加密后的 credentials 文件本身可以提交。
/db/*.sqlite3/db/*.sqlite3-journal/db/*.sqlite3-[0-9]*开发用数据库开发和测试用的 SQLite 文件。提交 db/schema.rb 和迁移文件,数据文件由各自生成。
/public/assets/public/packs/public/packs-test/public/system/public/uploads预编译资源与包assets:precompile 和 Webpacker 生成的结果以及上传文件。在部署过程中重新生成。
/.bundle/vendor/bundlenode_modules//vendor/assets/bower_components/yarn-error.log.yarn-integrityBundler 与 Node 依赖安装后会重新生成的 gem 和 JavaScript 包,以及安装日志。
/coverage//spec/tmpcapybara-*.htmlrerun.txtpickle-email-*.html.rspec测试副产物覆盖率结果、Capybara 失败时保存的页面、Cucumber 重跑列表。.rspec 被视为个人的 RSpec 选项文件。
.byebug_history.rvmrc.powenv*.rbc*.orig个人工具设置调试器历史、旧的 RVM 与 Pow 设置,以及合并工具留下的原始文件备份。

实践中的注意事项

原始模板

Rails.gitignore规则 38 行
*.rbccapybara-*.html.rspec/db/*.sqlite3/db/*.sqlite3-journal/db/*.sqlite3-[0-9]*/public/system/coverage//spec/tmp*.origrerun.txtpickle-email-*.html# Ignore all logfiles and tempfiles./log/*/tmp/*!/log/.keep!/tmp/.keep# TODO Comment out this rule if you are OK with secrets being uploaded to the repoconfig/initializers/secret_token.rbconfig/master.key# Only include if you have production secrets in this file, which is no longer a Rails default# config/secrets.yml# dotenv, dotenv-rails# TODO Comment out these rules if environment variables can be committed.env.env*.local## Environment normalization:/.bundle/vendor/bundle# these should all be checked in to normalize 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*.bowerrcbower.json# Ignore pow environment settings.powenv# Ignore Byebug command history file..byebug_history# Ignore node_modulesnode_modules/# Ignore precompiled javascript packs/public/packs/public/packs-test/public/assets# Ignore yarn files/yarn-error.logyarn-debug.log*.yarn-integrity# Ignore uploaded files in development/storage/*!/storage/.keep/public/uploads

模板来源: github/gitignore/Rails.gitignore @356fd7b (2026-09-11) · CC0-1.0

常搭配使用的模板

其他模板详解