技術と本について書くブログ

技術と本について書くblog。技術メモなど雑記を書いているblog。

GitHubのsubmoduleの追加方法、更新方法

GitHubのサブモジュールとは

自分が開発しているプロジェクトで他のプロジェクトを使用した時に使う。 Git submoduleを使えばリポジトリのサブディレクトリとして使え、それぞれ別プロジェクトとして管理ができる

サブモジュールの作り方

git submodule add リポジトリ名 ディレクトリ名

$ git submodule add https://github.com/hoge/hoge.git hoge 

リポジトリを指定する場合は-bオプションをつける

$ git submodule add -b master https://github.com/hoge/hoge.git

これで.gitmodulesが作られます。

サブモジュールのブランチの変更方法

サブモジュールのブランチ名 -aオプションでサブモジュールのブランチもわかるので普通にcheckoutすれば良い

$ git branch -a

サブモジュールの更新

$ git pull -f

https://hacknote.jp/archives/8905/

$ git submodule foreach git pull origin master

サブモジュールの削除

$ git submodule deinit path
$ git rm groupedTool

エラーが出たら指示通り-fオプションなどで消す もしsubmoduleのディレクトリ.git/modules/以下、.gitmodulesなどが残っていた場合、を消しておく

changes not staged for commit が出ていてPUSHできない場合 git add . してからcommit

参考

https://git-scm.com/book/ja/v1/Git-%E3%81%AE%E3%81%95%E3%81%BE%E3%81%96%E3%81%BE%E3%81%AA%E3%83%84%E3%83%BC%E3%83%AB-%E3%82%B5%E3%83%96%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB

https://qiita.com/sotarok/items/0d525e568a6088f6f6bb

https://qiita.com/noraworld/items/89ad0faefb048eda0870

https://qiita.com/u1aryz/items/8d1923da79158439eeaa