LoginSignup
2
1

More than 1 year has passed since last update.

GitでSubmoduleを含むリポジトリをCloneできないときの対処方法

Last updated at Posted at 2022-03-14

先日友人と共同作業をするために、友人のGitリポジトリをCloneする機会がありました。

このリポジトリにはSubmoduleが含まれていたので、

$ git clone --recursive リポジトリ.git

の形でcloneしようとしたのですが、見事にエラーになりました。

Submodule 'サブモジュール名' (git@github.com:サブモジュールの場所.git) registered for path 'サブモジュール名'
Cloning into 'ローカルのパス'...
Host key verification failed.
fatal: Could not read from remote repository.

と言われてしまいました。

ちなみに、GitHub DesktopでCloneした場合は以下のようなメッセージが出ます。

Clone failed.txt
Authentication failed. Some common reasons include:

- You are not logged in to your account: see File > Options.
- You may need to log out and log back in to refresh your token.
- You do not have permission to access this repository.
- The repository is archived on GitHub. Check the repository settings to confirm you are still permitted to push commits.
- If you use SSH authentication, check that your key is added to the ssh-agent and associated with your account.
- If you use SSH authentication, ensure the host key verification passes for your repository hosting service.
- If you used username / password authentication, you might need to use a Personal Access Token instead of your account password. Check the documentation of your repository hosting service.

環境・状況

  • 対象はGitHubのPublicリポジトリ
  • submoduleもPublic
  • リポジトリを作成した側は特に問題なく使えている

原因:sshでcloneしようとして認証ができない

SubmoduleのCloneができない理由の全てがコレ、ではないですが、私の場合はcloneの方法がsshになっていたことが原因でした。

解決方法

sshでなくhttpsでcloneするように変更します。

そのためには

  1. 一度submoduleの中身以外をclone
  2. 設定を一部書き換え
  3. submodule部分を含めてclone

という流れで行います。

cloneする

すでに上に書いたようなgit clone --recursive ~~~が済んでいるようであれば、submoduleの中身以外はcloneされた状態にあっているので、済です。

まだの場合はcloneしてください。

設定を書き換える

.gitがあるディレクトリ、.gitignoreを同じ階層に、.gitmodules`というファイルを作成します。

中に以下情報を書き込みます。

.gitmodules
[submodule "marpstyle"]
	path = marpstyle
	url = https://github.com/cunhapaulo/marpstyle.git

ここではmarpstyleの例を出していますが、pathにはsubmoduleが入ったディレクトリへのパスを、urlはsubmoduleのclone先を記載します。

もし既に.gitmodulesが存在していた場合、urlで指定しているものがgit@github~のように記載されていないか確認してみてください。sshで接続する指定になっている可能性があります。

submoduleを含めてclone

以下を実行

$ git submodule update --init --recursive

参考

2
1
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
1