LoginSignup
2
2

More than 1 year has passed since last update.

git submodule 追加・削除のやり方【2022 年版】

Last updated at Posted at 2022-11-30

「git submodule 削除」でやり方を調べると何通りか方法が見つかりますが、中には古いものもあったりして紛らわしいので現在のやり方をまとめてみようと思います。

検証バージョン

$ git --version
git version 2.38.1.windows.1

準備

サブモジュールとして追加したいリポジトリのクローン URL を用意してください。
ここではクローン URL を https://github.com/ryangrose/easy-pandoc-templates.git とします。
また、クローン先のパスを docs/templates/easy-pandoc-templates とします。

以下の変数を実際のリポジトリやパスに変えると、その後のコマンドがコピペで実行できます。
ぜひお試しください。

CLONE_URL="https://github.com/ryangrose/easy-pandoc-templates.git"
CLONE_PATH="docs/templates/easy-pandoc-templates"

以後のコマンドは、すべて親リポジトリのルートディレクトリで実行します。

操作

追加する

以下のコマンドを実行します。

git submodule add ${CLONE_URL} ${CLONE_PATH}

削除する

以下のコマンドで削除できます。
削除後は忘れずにコミットを行ってください。

git rm ${CLONE_PATH}

Deleted submodule: A submodule can be deleted by running git rm <submodule path> && git commit. This can be undone using git revert.

これで削除は完了なのですが、Git のディレクトリ内部には保持しているようです。
これにより、削除したサブモジュールを再度追加したい場合に、別のリポジトリからフェッチすることなくチェックアウトできるようになっています。

The submodule’s working directory is removed from the file system, but the Git directory is kept around as it to make it possible to checkout past commits without requiring fetching from another repository.

Git のディレクトリ内部からも完全に削除したい場合、以下のコマンドを実行します。

rm -rf .git/modules/${CLONE_PATH}
git config --remove-section submodule.${CLONE_PATH}

再び追加する

追加のコマンドをもう一度実行します。

git submodule add ${CLONE_URL} ${CLONE_PATH}

Git のディレクトリ内部にリポジトリの内容が残っている場合、以下のようなエラーメッセージが表示されます。
Git のディレクトリ内部まで完全に削除した場合は表示されません。

fatal: A git directory for 'docs/templates/easy-pandoc-templates' is found locally with remote(s):
  origin        https://github.com/ryangrose/easy-pandoc-templates.git
If you want to reuse this local git directory instead of cloning again from
  https://github.com/ryangrose/easy-pandoc-templates.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.

過去に削除したモジュールをもう一度同じパスに取り込む場合は --force を付けて実行します。

git submodule add --force ${CLONE_URL} ${CLONE_PATH}

出力

Reactivating local git directory for submodule 'docs/templates/easy-pandoc-templates'

参考にした記事

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