git submoduleの正しい削除方法

結論 以下2点が重要 .gitmodulesを触らない オリジナルリポジトリのgit履歴からも削除 方法 サブモジュール自体を削除する場合は.git/modules/<submodule_name>/も削除する必要がある .gitmodulesからも消えないのはgit rm -rでサブモジュールを削除していないから # サブモジュールを解除 $ git submodule deinit submodule-A Cleared directory 'submodule-A' # git履歴から削除(※これ重要で.gitmodulesから消えてくれる) $ git rm -r submodule-A/ # .git以下からも削除(※これ重要。面倒くさい) $ rm -rf .git/modules/submodule-A 正しく削除できていない時に発生するエラー その1 'submodule-A' already exists in the index その2 "submodule" already exists in the index

October 12, 2023 · Me

git submoduleコマンドの理解

参考 git-scm.com git-submodule git-scm.com - 7.11 Git のさまざまなツール - サブモジュール git submoduleコマンドオプション一覧 add … サブモジュールを追加 .gitsubmodulesファイルに追記される(存在しなければ生成) タグ指定やコミットハッシュ指定ができないので注意 $ git submodule add --name submodule-A --branch main --depth 1 https://github.com/example/submodule-A.git $ cat .gitmodules [submodule "submodule-A"] path = submodule-A url = https://github.com/example/submodule-A.git branch = main タグ、コミットハッシュ指定する 参考: https://stackoverflow.com/questions/1777854/how-can-i-specify-a-branch-tag-when-adding-a-git-submodule $ cd submodule-A git checkout v1.0 cd .. git add submodule-A git commit -m "moved submodule to v1.0" git push [発生するエラー] 既に追加されている(.submodulesから手動削除しても) 'submodule-A' already exists in the index 手動で削除したりすると怒られる。空ファイルを作成して再実行 please make sure that the ....

October 12, 2023 · Me