github/gitignore · Rails.gitignore
Ruby on Rails .gitignore テンプレート解説
Rails.gitignore が log・tmp・storage、開発用 SQLite、master.key、プリコンパイル済みアセット、node_modules を無視する理由と、.keep の否定パターンの構造を説明します。
Rails.gitignore は Rails アプリケーションの実行中に生まれるファイルと秘密情報を防ぐ。ログ・一時ファイル・アップロードファイルのようにサーバーの動作とともに蓄積するデータと、アセットパイプラインが作る成果物が主な対象である。
/log/* と !/log/.keep のように、フォルダーの中身だけを除外し、空の目印ファイルは残すパターンが 3 回登場する。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-integrity | Bundler・Node の依存関係インストールすれば再生成される gem と JavaScript パッケージ、インストールログである。 |
/coverage//spec/tmpcapybara-*.htmlrerun.txtpickle-email-*.html.rspec | テストの副産物カバレッジの結果、Capybara が失敗時に保存するページ、Cucumber の再実行リストである。.rspec は個人の RSpec オプションファイルとして扱う。 |
.byebug_history.rvmrc.powenv*.rbc*.orig | 個人のツール設定デバッガーの履歴、古い RVM・Pow の設定、マージツールが残した元ファイルのバックアップである。 |
実務での注意点
- テンプレートのコメントのとおり、
Gemfile.lock、.ruby-versionはコミットしてすべての環境のバージョンをそろえる。 .rspecをチーム共通の設定として使うなら、この行を削除する。rails newが作る既定のファイルもコミットすることが多い。config/master.keyをすでにコミットしている場合は、鍵を新しく作り(credentials の再暗号化)、以前の鍵で暗号化していた値を差し替える。
元のテンプレート
Rails.gitignore
*.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