I have a setup with multiple ssh keys for multiple sources/users with different keys. To set up such environment I have followed various guides such as: Multiple GitHub accounts on the same computer? to do the set up.
To recap what happens: basically I have multiple ssh keys on my computer, for example I have my personal github, my personal bitbucket, work bitbucket. Each one of those set up with a made-up Host
so that I can clone those using automatically different keys, as outlined in the above mentioned article.
For each remote the configured key works as it should when I am in a new shell, for example I can do git clone/push/pull with no problem with any of those. However, if within the same shell I cd into a repo that needs a different key and I try any git command this one fails with the error:
The requested repository either does not exist or you do not have access. If you believe this repository exists and you have access, make sure you're authenticated.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
However if I run ssh -t git@bit_personal
(made up host) or ssh -t git@hvhr
(another made up host) or ssh -t git@github.com
it will work fine within the same shell.
To make the git push/pull etc command work a workaround I found is to run ssh-add -D
before running it.
This is what my ssh config file looks like:
Host github.com
IdentityFile ~/.ssh/id_github
Host bit_personal
HostName bitbucket.org
IdentityFile ~/.ssh/id_bitbucket_personal
Host hvhr
HostName bitbucket.org
IdentityFile ~/.ssh/id_bitbucket_hvhr
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_general
Running a git remote -v
in the bit_personal
repo I get:
origin git@bit_personal:my_username/dot-files-dump.git (fetch)
origin git@bit_personal:my_username/dot-files-dump.git (push)
and git remote -v
in the company remote:
origin git@hvhr:my_username/repo.git (fetch)
origin git@hvhr:my_username/repo.git (push)
Successful request response from a matching bit_personal git remote debugged with GIT_SSH_COMMAND="ssh -vvv" git pull and here an unsuccessful request from the same repo. (Note: to reproduce in the same terminal I did a git pull
of a repo matching hvhr
. The main difference that I can see between the two requests is that the order of the Will attempt key
is incorrect when it fails. When the request is successful I get this in the debug lines:
debug1: Will attempt key: /Users/mimo/.ssh/id_bitbucket_personal ED25519 SHA256: explicit agent
debug1: Will attempt key: /Users/mimo/.ssh/id_ed25519 ED25519 SHA256: explicit
While when unsuccessful it uses the key matching the Host *
key (which was successful at a previous pull in the same terminal):
debug1: Will attempt key: /Users/mimo/.ssh/id_ed25519 ED25519 SHA256:Qj6a9wU+leduocucbcjohcjakokghwpidtpllwbu explicit agent
debug1: Will attempt key: /Users/mimo/.ssh/id_bitbucket_personal ED25519 SHA256:89otR+uzlbkcjkjxyifswgnwbdrlefssnwanbe explicit
git version 2.38.0
I'm using Mac OS 12.6, with ARM CPU, I tried this on different shells: zsh, sh, bash, all showing the same behavior.