I have two github accounts on my computer: one for work and one for my personal account. (I'm on a mac) My .ssh config file:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host github.com-personalAccount
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_personal
This works fine. I added the ssh key to my github account. I can push to my personal repos just fine, and push to work repos as well.
Now, I've been invited as a collaborator to a third repo, and I've accepted the invitation. I figured I could use my personal ssh key:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host github.com-personalAccount
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_personal
Host github.com-otherUserAccount
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_personal
Then, in the repo:
git remote add origin [email protected]:otherUserAccount/redacted.git
git add .
git commit -m "Initial commit"
git push -u origin master
But it doesn't work. I get this error:
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I've double and triple checked. The repo address is correct.
Please help!
Thanks in advance!
CodePudding user response:
Git doesn't pass to SSH repository name, it only passes user@hostname
. So for SSH to use the correct key Git must pass the correct hostname which it takes from the remote URL. With your .ssh/config
the remote URLs must be (in terms of Git commands):
git remote set-url origin [email protected]:personalAccount/personalRepo.git
and
git remote set-url origin [email protected]:otherUserAccount/otherRepo.git