参考にさせていただいたページ
.gitignoreについて
- gitignore/Rails.gitignore at main · github/gitignore · GitHub
- ちょっと待った! Railsでgitリポジトリから除外すべきでないファイル:Gemfile.lockとdb/schema.rb|TechRacho by BPS株式会社
config/environmentsについて
今日の環境
はじめに
gitリポジトリに
- 入れるべきファイル
- 入れた方が良いファイル
- 入れない方が良いファイル
- 入れては駄目なファイル
よくわからないなー。と思いつつ、合っている自信はまったく無いけど、ちょこちょこ調べつつ.gitignoreを編集します。
合っているかわからないので、もしこの記事を参考にしているという方がいらっしゃったら、ご自身の責任で.gitignoreの編集をしてください。
私は一切責任を負いません。
railsのスタートページを表示させるところまでやる
以前書いた「Docker Composeを使ってローカルでRails7のSSL環境構築 | Mac + Docker + Rails その0006 - web_bonsaiの日記」のboilerplateを使用して、web_bonsaiディレクトリ内でrailsのスタートページを表示するところまでやります。
rails/.gitignoreを一度commitする
rails newしたときに rails/.gitignore が生成されましたので一度commitします。
GitHubで生成した.gitignoreと合わせてみる
「開発用のリポジトリを作成してcloneして準備をする | Mac + Docker + Rails その0009 - web_bonsaiの日記」で保存しておいた「.gitignore_by_git_hub」を使って、一旦rails/.gitignore
を上書きして差分を確認してみます。
差分が確認できたら、足りない項目を rails new
で生成された.gitignoreに追記しておきます。
.gitignore_by_git_hubの方には、以下の記述がありましたので、rails/.gitignoreに追記しておきました。
# Ignore pidfiles, but keep the directory. /tmp/pids/* !/tmp/pids/ !/tmp/pids/.keep # Ignore uploaded files in development. /storage/* !/storage/.keep /tmp/storage/* !/tmp/storage/ !/tmp/storage/.keep
rails/.gitignoreをもう一度commitする
もう一度commitします。
これでGitHubで生成した.gitignoreとrails newで生成した.gitignoreがマージできました。
gitignoreリポジトリの内容も合わせてみる
「gitignore/Rails.gitignore at main · github/gitignore · GitHub」の内容をrails/.gitignoreにコピペしてみます。
差分を確認すると足りない項目があったので一応追記しておきます。
以下の行を .env
の下に追記しました。
.env*.local
以下の行を !/storage/.keep
の下に追記しました。
/public/uploads
gitリポジトリに入れては駄目なファイルについてもう少し調べたりする
参考にさせていただいた「ちょっと待った! Railsでgitリポジトリから除外すべきでないファイル:Gemfile.lockとdb/schema.rb|TechRacho by BPS株式会社」によると、以下のファイルが挙げられていますが2014年の記事なのでさらっと参考程度に思っておきます。
- .rvmrc
- config/database.yml
gitリポジトリに入れては駄目なファイルがまだよくわからないので、コツコツaddしてcommitしつつ、「これは駄目かもしれない。」と思うファイルを自分なりにリストアップしてみます。
rails/config/master.key
などの明らかに入れては駄目なファイルは、既にrails/.gitignoreに記述されているので git status
したときに Untracked files にはリストアップされませんし、addされません。
ファイルの中身を確認したり、ファイルの役割をwebで検索したりしてみましたが、とりあえず rails/config/database.yml
を追記すれば今の段階では良さそうに感じました。
railsについてもう少し学習してから再考したいと思います。
.gitignoreにdatabase.ymlを追記する
rails/.gitignoreに以下の通りdatabase.ymlを追加しておきます。
config/database.yml
database.sample.ymlの作成
以下のコマンドで database.yml.sample を作成します。
cp rails/config/database.yml rails/config/database.sample.yml
commitしておきます。
最終的に作成したrails/.gitignore
とりあえず以下の通りになりました。
*.rbc capybara-*.html .rspec /db/*.sqlite3 /db/*.sqlite3-journal /db/*.sqlite3-[0-9]* /public/system /coverage/ /spec/tmp *.orig rerun.txt pickle-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 repo config/initializers/secret_token.rb config/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 *.bowerrc bower.json # Ignore pow environment settings .powenv # Ignore Byebug command history file. .byebug_history # Ignore node_modules node_modules/ # Ignore precompiled javascript packs /public/packs /public/packs-test /public/assets # Ignore yarn files /yarn-error.log yarn-debug.log* .yarn-integrity # Ignore uploaded files in development /storage/* !/storage/.keep /public/uploads # ここより上はrails newで生成された.gitignoreの記述 # ここより下はGitHubでリポジトリ作成時に生成した方の.gitignoreにだけあった記述 # Ignore pidfiles, but keep the directory. /tmp/pids/* !/tmp/pids/ !/tmp/pids/.keep # Ignore uploaded files in development. /storage/* !/storage/.keep /tmp/storage/* !/tmp/storage/ !/tmp/storage/.keep ## ここより下は自分の判断で個人的に追記したもの config/database.yml config/credentials.yml.enc