LoginSignup
0
0

More than 3 years have passed since last update.

submoduleの役割と使い方の備忘録

Last updated at Posted at 2021-04-08

Submodule知らないとハマる

らしい。
ということで調べたことをまとめた備忘録です。

基本git公式の焼きまわしの二番煎じしか書いていないので
より詳しいことはリンク飛んでください。

こちらの記事が大変わかりやすかったので参考にさせていただきました。
もうぶっちゃけこっちを見とけばいいと思いますが、必要最低限のことをより端的にまとめました。

概要

  • 登場人物:親(主ブランチ)、子(別レポジトリのコミット)
  • 役割 :親(主ブランチ)が子(別レポジトリのコミット)を参照する。
  • 注意点 :
    • 参照はコミット単位。ブランチ単位ではない。
    • 子を自動で追跡はしない。明示的に参照を変える。
    • サブモジュールのあるブランチから無いブランチに切り替えると「追跡されていないディレクトリ(working treeにある状態)」として残ってしまう。
    • 親からサブモジュールへの切り離しは、親の該当ディレクトリをアンステージしてから。

git submodule

  • git標準機能
  • 関連ファイル
    • .gitmodules
    • .git/config
    • インデックスとワーキングツリー

使い方

主にやれることは

1. サブモジュール追加
2. サブモジュール変更(リビジョンの変更)
3. サブモジュール削除
4. サブモジュール付きクローン
5. (リモートでのみ)サブモジュール参照している場合、ローカルでも参照させる




  • サブモジュールを追加する時
$ git submodule add https://example.com/sub-modules # .gitmodules ファイルが更新される
$ git commit -a # 変更を保存(必須!)
  • サブモジュールのリビジョンを変える
$ cd sub-module # サブモジュールのディレクトリに移動
$ git fetch && git reset --hard origin/master # 適当なコマンドで、必要なバージョンをチェックアウトする

$ cd ../ # 親側に移動
$ git commit -a # 親側でコミット
  • サブモジュールを削除する
$ git submodule deinit -f sub-module # 登録解除
$ git rm -f sub-module # ファイルを削除
$ git config -f .gitmodules --remove-section submodule.sub-module # 設定ファイルから削除
$ git commit -a # 変更を保存(必須!)
  • サブモジュール付きクローン --recursiveを付ける。
$ git clone https://example.com/repo-with-sub-modules --recursive
  • (リモートでのみ)サブモジュール参照している場合、ローカルでも参照させる
$ git submodule update --init
  • どのサブモジュールを参照しているか
git submodule status
リビジョン?

コミットは動作、リビジョンはコミットされた状態の単位
https://qiita.com/YumaInaura/items/dc9e582d7096f54e649b

時々見返すメモ

  • -dirty とは何か
$ git diff
-Subproject commit <コミット番号>
+Subproject commit <コミット番号>-dirty

特に触った覚えのないsubmoduleに-dirty付きで差分ができていた。
追跡されていない、もしくはsubmoduleに変更が加わった時に表示される。
IDEの一括変換などでウッカリ変更しちゃうなどもある。

Submodules are now regarded as dirty if they have any modified files or untracked files, whereas previously it would only be the case if HEAD in the submodule pointed to the wrong commit. Why is this annoying? Submodules are now regarded as dirty if they have any modified files or untracked files, whereas previously it would only be the case if HEAD in the submodule pointed to the wrong commit. Why is this annoying?

参照

submoduleとは/使い所/チートシート
7.11 Git のさまざまなツール - サブモジュール

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0