How to propagate the core.sshCommand option to submodule update?
Asked Answered
C

3

7

I've set the core.sshCommand option for a repo so that I could use a different ssh key when working with it (i.e. sshCommand = ssh -i /path/to/key). However, when I run git submodule update this option is not considered:

fatal: Could not read from remote repository.

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

Is there any way I can configure the repo to use the given ssh key for itself and any submodules?

Charmainecharmane answered 26/8, 2019 at 11:32 Comment(6)
Were you able to solve this?Fabian
@ArkaPravaBasu Did my answer help?Dolley
Hello @JohnStrood, I followed this to solve this.Fabian
@ArkaPravaBasu Yes, you can do that too, but that won't help you if you want to set it locally, like if you want to clone different repos to different github accounts.Dolley
@ArkaPravaBasu Although you can ask ssh to try all keys in succession. It would be more elegant to set the env GIT_SSH_COMMAND when you pull/push.Dolley
Reading the git docs it would seem you can only use core.sshCommand for git fetch and git push.Blinnie
K
11

Either set it globally:

git config --global core.sshCommand "ssh -i /path/to/key"

But that sets the key for every repository you work with.

Or set it for every submodule:

git submodule foreach git config core.sshCommand "ssh -i /path/to/key"
Kirkland answered 26/8, 2019 at 12:2 Comment(2)
Not working for me. Unable to add a submodule as it is picking the wrong ssh keyFabian
git submodule foreach runs through the existing submodules. For a new submodule just being added you need something like git -c core.sshCommand="ssh -i /path/to/key" submodule addKirkland
D
5

According to this answer, from where I pulled this quote,

The core.sshCommand option was added to git 2.10.0. You're running 2.7.4, which doesn't have that functionality. You'd need to upgrade to a newer Git if you wanted to use it.

If you are running a very old version, be mindful of the changes. You can consult this changelog.

You can use the environmental variable GIT_SSH_COMMAND instead.

export GIT_SSH_COMMAND="/usr/bin/ssh -i /your/path/to/id_rsa"
Dolley answered 7/8, 2020 at 10:32 Comment(0)
B
1

This used to work for me:

git config --global core.sshCommand "ssh -i /path/to/key"

However, for some weird reason it suddenly stopped working on my Windows11 machine after a windows update around September 2024 (I suspect it has something to do with OpenSSH).

Even the command 'ssh -i' no longer works properly (it doesn't add the key-file at all!)

In any case I resolved all this by tweaking the 'core.sshCommand' section in the local .git/config of my repo (you can also do it globally in your ~/.gitconfig file if you have a single key file) so as to have it add the key via 'ssh-add' and to explicitly call ssh.exe like so:

[core]
    (...)
    sshCommand = ssh-add  ~/.ssh/key.ppk  2>/dev/null   &&    'C:\\Windows\\System32\\OpenSSH\\ssh.exe'

Once I did this everything went back to normal on my Windows11 machine. Just my 2c.

PS: Using git ver. 2.46.2.windows.1 (installed system-wide)

Brink answered 27/9 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.