LoginSignup
2
2

More than 3 years have passed since last update.

【Git】サブモジュール をコミットした後の親プロジェクトの対応方法

Posted at

個人メモです。
サブモジュールで更新した後に、親のプロジェクトでどう対応したらいいか迷うので、まとめとして。

サブモジュールのあるプロジェクトで、サブモジュールを更新すると、親のプロジェクトでステータスを見ると、

ディレクトリ名(new commits)

が表示される。この後の処理方法などについて。

$git status

modified:   submodule名 (new commits)

または、

$git status

modified:   submodule名 (modified content)

1. submoduleで変更をコミットする。

submoduleで変更を加えたら、git add, git commitする。

2. submoduleのmasterにマージする

親のプロジェクトに反映する前に、submoduleの変更内容をmasterにマージする。

submoduleのディレクトリで、

$ git push origin master

または、個人のブランチにpushし、プルリクを出す。

3. 親のプロジェクトディレクトリでコミットの確認

サブモジュールの変更内容を親のプロジェクトに反映させるため、ルートのプロジェクトディレクトリに移動する。

git-status
#submoduleが直下にあった場合
$ cd ..   

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
        modified:   submodule名 (modified content)

no changes added to commit (use "git add" and/or "git commit -a")

commit or discard the untracked or modified content in submodulesとあり、サブモジュールで変更があったことを伝えてくれる。


3. git diffで詳細を確認

$ git diff
diff --git a/sbmodule名 b/sbmodule名
--- a/sbmodule名
+++ b/sbmodule名
@@ -1 +1 @@
-Subproject commit 8f71500980389a8cdd5522545d352df5fa7b4faf
+Subproject commit 8f71500980389a8cdd5522545d352df5fa7b4faf-dirty

Subprojectでコミットの変更があったことだけが表示される。

サブモジュールのファイルの変更内容は見えない。中身を確認するためには、サブモジュールのディレクトリに移動する必要がある。


4. サブモジュールの変更をコミット

サブモジュールの変更内容を確認し問題がなければ、

親プロジェクトで、サブモジュールの変更があったことをステージ&コミットする。

$ git commit -m "update submodule"

コミット名は細かく記述してもいいが、サブモジュールで変更が頻繁にある場合は「update submodule」で簡略化してもいい。(コミット履歴はサブモジュールに行けば見れるため)

$ git add submodule名

$ git commit -m "update submodule"
[master 734fec6] update submodule
 1 file changed, 1 insertion(+), 1 deletion(-)


#クリーンになったか確認
$ git status
On branch master
nothing to commit, working tree clean
2
2
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
2
2