git submodule の使い方まとめ
git submodule は、本体ソース内から、別のリポジトリを参照する場合に使用する。
git submodule | show と同じ | |
status | サブモジュール一覧を表示する。プレフィックスの意味 - : 未初期化, + : インデックスのSHA-1と合っていない, U:コンフリクト発生 | |
add | サブモジュールを追加する。その後commit をする必要がある。 | |
init | .gitmoduleの情報(名前とレポジトリのurl)を .git/config に書き出す | |
update | .git/config の情報を元にsubmoduleをクローンする |
git submodule init してから、git submodule updateするのと、git submodule update --init は同じ。
git submodule init で追加される情報は以下のとおり
[submodule "cap_test"] url = git@github.com:u16suzu/cap_test.git
git submodule init のヘルプ和訳
Initialize the submodules, i.e. register each submodule name and url found in .gitmodules into .git/config. The key used in .git/config is submodule.$name.url. This command does not alter existing information in .git/config. You can then customize the submodule clone URLs in .git/config for your local setup and proceed to git submodule update; you can also just use git submodule update --init without the explicit init step if you do not intend to customize any submodule locations.
git commit init コマンドはサブモジュールを初期化する。つまり、.gitmodules にあるサブモジュールの名前とurlを .git/config に書きだす。 .git/config で使用されるキーは submodule.$name.url である。このコマンドは .git/config の既存のデータを書き換えない。したがって、.git/config にあるURLはローカルのものを指定でき、git submodule update することができる。どのサブモジュールのパスも変更しないならば、 git submodule update --init を使うことで、git submodule init を省略できるよ。
サブモジュールを編集する
サブモジュールで参照した先のファイルを編集した場合は参照元のcommitも行う必要がある。これをしないと、どのコミットを参照しているかわからなくなるらしい。もしくは、submodule の本体レポジトリでコミット&pushし、参照元でpullする。最初は後者の方をやってたけど、大分面倒くさかったので前者に変えた。
前者の場合に、参照元の更新を行わないと、git submodule status のプリフィックス + がでる。
git submodule update するか、上の方法で解決する。
サブモジュールの削除を行うコマンドはない。以下削除の手順。
.gitmodules, .git/config から該当する行を削除後 git rm --cached path/to/hoge git commit -m "Del: delete submodule"