Ikkyu's Tech Blog

技術系に関するブログです。

Rails TutorialでHerokuにデプロイする時に、サブディレクトリを指定する方法

以前さわっていたRails...2年前ぐらいだったので、Rails再入門しようと思って
Rails Tutorialをやりはじめました。

TutorialではHerokuへのデプロイがあります。

Docker使ってTutorialやろうと思っていたので、
ディレクトリは以下のような構成になっています。

.
├── README.md
├── docker
├── tools
└── tutorial
    └── environment
        └── hello_app

Turorialは、この構成ではないので、

$ git push heroku master

を実行するとデプロイに失敗します。

やりたいことは、

tutorial/environment/hello_appをHerokuにデプロイしたいです。

そこで、git subtree push使って、サブディレクトリをHerokuへpushします。

git subtreeが気になる方は以下を参照してください。 github.com

今回の私のケースですと

$ git subtree push --prefix tutorial/environment/hello_app/ heroku master 

で、hello_appがHerokuにデプロイできました!!

git便利じゃん!ーって思ったけど、知らないこといっぱいだな...

また一つ勉強になりました。

subtree pushする時にエラーが発生する場合

push時に以下のエラーが発生する場合があります。

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

その場合は、以下のようにすればデプロイできす。

git push heroku `git subtree split --prefix [path name] master`:master --force

参考記事
Herokuでサブディレクトリをデプロイできるらしいのでメモ | Qrunch(クランチ)
[Heroku] サブディレクトリだけを deploy する | CodeNote
Git subtree で push できないときの対処法 - Qiita
Why can't I push this up-to-date Git subtree? - Stack Overflow