How to use SSH instead of HTTP for Git submodules?
Asked Answered
P

3

8

Why are people having this problem?

$ git clone --recursive [email protected]:acani/Chats.git Cloning into 'Chats'... Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

https://github.com/acani/Chats/issues/53#issuecomment-118014684

I've read some answers that say to change the submodule URL from SSH to HTTP, but why should I have to do that. I don't want to do that. I want to keep it SSH so that I don't have to enter my username & password into Terminal if I want to push. Everyone can clone the SSH URL fine, so why can't they recursively clone it as a submodule?

Proctor answered 2/7, 2015 at 12:38 Comment(4)
Possible duplicate: #25957625Tremayne
@ma1 Did I answered your question with the workaround I proposed below?Incidentally
@Incidentally Sorry, I didn't try your workaround. I just switched from the SSH protocol to the Git protocol. Thank you for answering anyway though.Proctor
Without changing the .gitmodules file you could instead change your ~/.gitconfig to use SSH instead of HTTP in URLs, as answered in https://mcmap.net/q/166835/-automatically-access-git-submodules-via-ssh-or-httpsTerminal
P
4

The SSH protocol doesn’t support anonymous access to a Git repository.

So, don't use the SSH protocol. Instead, use either the Smart HTTP protocol (recommended) or the Git protocol.

For every submodule URL in your repository's .gitmodules file, replace [email protected]: with either https://github.com/ (to use the Smart HTTP protocol) or git://github.com/ (to use the Git protocol).

More info: Git - The Protocols

Proctor answered 19/12, 2018 at 16:28 Comment(3)
So my answer is valid: use https instead of SSH for submodulesIncidentally
Exactly: my answer is about not changing anything, but still using the right protocol.Incidentally
once we update the ssh urls to https urls in .gitmodules files what is the procedure that needs to be followed since even after updating to https urls I am unable to clone submodules with git submodule update --init --recursive command. I further checked that my main repo config file still has ssh url for the submodule.Vu
I
8

As a workaround, you can try using https url for any github repo:

cd myParentRepo
git config url.https://github.com/.insteadOf ssh://[email protected]/
# or
git config url.https://github.com/.insteadOf [email protected]:
Incidentally answered 2/7, 2015 at 12:47 Comment(0)
P
4

The SSH protocol doesn’t support anonymous access to a Git repository.

So, don't use the SSH protocol. Instead, use either the Smart HTTP protocol (recommended) or the Git protocol.

For every submodule URL in your repository's .gitmodules file, replace [email protected]: with either https://github.com/ (to use the Smart HTTP protocol) or git://github.com/ (to use the Git protocol).

More info: Git - The Protocols

Proctor answered 19/12, 2018 at 16:28 Comment(3)
So my answer is valid: use https instead of SSH for submodulesIncidentally
Exactly: my answer is about not changing anything, but still using the right protocol.Incidentally
once we update the ssh urls to https urls in .gitmodules files what is the procedure that needs to be followed since even after updating to https urls I am unable to clone submodules with git submodule update --init --recursive command. I further checked that my main repo config file still has ssh url for the submodule.Vu
R
0

This works for me in the .gitmodules file:

url = git+ssh://[email protected]/path-to-repo

Note: In case you already have used git submodule init you have to sync with git submodule sync. That way the submodule url's will update accordingly and you can call git submodule update to try to fetch the repository again.

Racemic answered 1/6, 2022 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.