gitでsubmoduleを削除する方法をご紹介!

一度gitで登録したサブモジュール(submodule)を削除する方法を紹介します。git submodule addした後すぐの場合や既にgitで登録済のサブモジュールを削除する方法を解説します。

コンテンツ [表示]

  1. 1gitでsubmoduleを削除したい
  2. 2gitでsubmoduleを削除する方法
  3. 2.1すでにリポジトリに登録済みのsubmodule
  4. 2.2まだリポジトリに登録していない

gitでsubmoduleを削除したい

gitでsubmoduleを導入したはいいが、後になって必要なくなったので削除したいというケースを考えます。

gitのsubmoduleは簡単に登録できる反面、削除は少しやっかいです。それぞれのケースでgitのsubmoduleの削除方法を紹介していきます。

Photo byPexels

gitでsubmoduleを削除する方法

ケースごとに紹介していきます。

すでにリポジトリに登録済みのsubmodule

すでにgitでリポジトリに登録済のsubmoduleは以下の手順で削除しましょう。

submoduleの削除

git submodule deinit -f サブモジュールのパス
git rm -f サブモジュールのパス
rm -rf .git/modules/サブモジュールのパス

まだリポジトリに登録していない

まだsubmoduleをリポジトリに登録していない場合(git submodule addしただけ)は下記の手順で実行しましょう。

submoduleの削除

git reset
rm -rf .git/modules/サブモジュールのパス

# .gitmodulesを戻す
git config --local --remove-section submodule.サブモジュールのパス
git config -f .gitmodules --remove-section submodule.サブモジュールのパス

# もしはじめてのサブモジュールであれば.gitmodulesも削除
rm .gitmodules
GeekHive採用サイト

関連記事