1

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.

Mimo
  • 6,015
  • 6
  • 36
  • 46
  • 1
    `host` in `ssh -Tv git@host` must be one from `Host` directive, not `HostName`. The same for `git remote`. – phd Oct 06 '22 at 08:51
  • 1
    Yes, that's why I typed in my queston `git@host` instead of `git@hostname`. – Mimo Oct 06 '22 at 21:37
  • 1
    `git remote -v` ? `GIT_SSH_COMMAND="ssh -vvv" git push` ? – phd Oct 07 '22 at 09:35
  • 1
    I added more details to the question with the `git remote -v` and the output of `GIT_SSH_COMMAND="ssh -vvv" git push`. Seems that the main difference between a successful and unsuccessful operation is that in the latter the attempted first key is going to be the last one used which was successful, but incorrect for the current repo. – Mimo Oct 08 '22 at 01:13
  • 1
    Try [`IdentitiesOnly yes`](https://stackoverflow.com/a/11251797/7976758) … https://stackoverflow.com/search?q=%5Bssh%5D+IdentitiesOnly – phd Oct 08 '22 at 08:37
  • Amazing, it was exactly what I needed to do for it to work :) – Mimo Oct 09 '22 at 22:22

1 Answers1

-1

you can create multiple git configs and use one config per repo if needed.

Guide to that:

https://www.freecodecamp.org/news/how-to-handle-multiple-git-configurations-in-one-machine/