LoginSignup
0
0

More than 3 years have passed since last update.

git submoduleの追加/削除/更新の練習する

Last updated at Posted at 2021-04-19

ドキュメント

認証が通らないパターン

please make sure you have the correct access rights and the repository exists. exit status 128

  • git cloneにHTTPつかってる場合
  • SSHKeyが未登録の場合

submodule追加の練習

  1. サブモジュールとして読み込むリポジトリ(child)を公開で作る
  2. 親リポジトリ(parent)に1.を追加する

※HTTPで追加すると更新に失敗するのでSSHを利用
※HTTPで追加した場合は、submodule削除して入れ直し

Terminal
git init child
cd child
touch README.md
git add .
git commit -m "first commit"
# GITHUB上でchildリポジトリを作る
git remote add origin git@github.com:<GITアカウント>/child.git
cd ../
git init parent
cd parent
git submodule add git@github.com:<GITアカウント>/child.git
git add .
git commit -m "first commit"

VSCODE上なら以下の様に登録されたサブモジュールが青で表示される
スクリーンショット 0003-04-19 17.49.24.png

Terminal
# GITHUB上でparentリポジトリを作る
git remote add origin git@github.com:<GITアカウント>/parent.git

GITHUB上で見ると、以下の様にサブモジュールにリンクされている
(@以降はcommit番号)
スクリーンショット 0003-04-19 17.54.10.png

マスター以外のブランチを使う場合

git submodule add -d <ブランチ名> <リポジトリ名>

submodule更新の練習

サブモジュール(child)を更新して親(parent)に反映

以下3つのやり方があるので、順に試す

  1. git submodule update --remote --merge
  2. git submodule update --remote
  3. git submodule update --recursive
  1. childREADME.mdを更新してPUSH
  2. parentで更新をPULLしてマージ
  3. parentをpush

README.mdを更新

cd ../child 
git add .
git commit -m "Update README.md"
git push
cd ../parent
git submodule update --remote --merge
# もしくは
# git submodule foreach git fetch
# git submodule foreach git merge origin/master
git add child
git commit -m "Update child"
git push

さらにREADME.mdを更新

# ここまで同じ
git submodule update --remote
# ここから同じ

さらにREADME.mdを更新

# ここまで同じ
git submodule foreach 'git pull origin master'
git submodule update --recursive
# ここから同じ

サブモジュールの解除

cd ../parent
rm -rf child
git submodule deinit child
rm .gitmodules
rm -rf .git/modules/child
error: the following file has local modifications:

ローカルリポジトリをサブモジュール

リポジトリ削除

GitHubから公開したchildを削除

参考

https://qiita.com/BlueSilverCat/items/19bb9b814572cd35b2ae
https://rochefort.hatenablog.com/entry/git_submodule_update_recursive

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