LoginSignup
26
16

More than 3 years have passed since last update.

git submoduleを再作成する時の注意

Last updated at Posted at 2016-09-24

削除後の再作成でハマったのでメモ。
と言っても推奨されない使い方でgit submodule add --forceした場合の話。

まず作成・削除

submoduleを作成する。

$ git submodule add <url> <path>

このコマンドで、以下が行われる。
+ .gitmodulesが作られ、submoduleの情報が追記される
+ .git/configにsubmoduleの情報が追記される
+ .git/modules/<path>が作られ、管理ファイルが置かれる
+ <url>のリポジトリが<path>にcloneされる

対して削除コマンドは

$ git submodule deinit <path>

これで以下が行われる。
+ .gitmodulesから指定submoduleの情報が削除される
+ .git/configから指定submoduleの情報が削除される

cloneされた実体が消えていないのでgit rmも行う。

$ git rm <path>

再作成

表面上は削除できたが、実は.git/modules/<path>が残っている。
そこで同じ<path>に対してsubmoduleを作ろうとするとエラーが返る。

$ git submodule add <url> <path>
A git directory for '<path>' is found locally with remote(s):
  origin        <url>
If you want to reuse this local git directory instead of cloning again from
  <url>
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.
  • 同じリポジトリについて再度submoduleを作りたいなら--forceを指定せよ
  • そうでないなら--name <name>で別名を付けろ

ということだ。

ダメなやり方: 異なるリポジトリなのに--forceで再作成

git submodule add --force <other-url> <path>

異なるリポジトリなのにムリヤリ--forceを指定すると、表面上はうまくいってしまう。
しかし管理ファイルは以前のリポジトリを参照したままなので、期待した動作にはならない。
git submodule statusなどでは違うステータスが見える筈。

良いやり方: 異なるリポジトリなので--nameで再作成

git submodule add --name <name> <other-url> <path>

この場合管理ファイル置き場は.git/modules/<name>になるので古いディレクトリとはぶつからない。

たぶんダメなやり方: 管理ファイルを削除してから再作成

.git/modules/<path>を削除してからなら--force--nameも無しで再作成できる。
しかし、たぶんこれはよくないやり方。弊害がありそう。

古いsubmoduleがあった時代をcheckoutできなくなったりするとか?
でも分散バージョン管理でそんなことあるかな。

ご存知の方教えてください

26
16
1

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
26
16