Home > Net >  How to configure .ssh/config to use two remote repos from the same account?
How to configure .ssh/config to use two remote repos from the same account?

Time:10-26

I have two projects and for each of them I use one github repo. Both repos belong to one github account. I have registered thessh keys of the corresponding remote repo in .ssh/config. Problem is that the push command works only for the repo, for one repo at a time:

IdentityFile ~/.ssh/id_rsa1

When I try to put mutliple ssh keys into config:

IdentityFile ~/.ssh/id_rsa1
IdentityFile ~/.ssh/id_rsa2

git throws this:

ERROR: Repository not found.
fatal: Could not read from remote repository.

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

When I do it like so, only the first key works:

Host github.com
IdentityFile ~/.ssh/id_rsa1

Host github.com-repo2
IdentityFile ~/.ssh/id_rsa2

How can I make both keys work without having to edit config each time I want to use the other repo?

Edit:

Trying to push with this config file:

Host github.com-repo1:me/reponame1
User git
IdentityFile ~/.ssh/id_repo1

Host github.com-repo2:me/reponame2
User git
IdentityFile ~/.ssh/id_repo2

command:

git push -u origin main

leads to the same 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.

CodePudding user response:

First, make sure to include below each of your Host entries:

User git

Second, the remote URL for each repository would then be:

github.com-repo1:me/repo1
github.com-repo2:me/repo2

That means, for instance, that ssh github.com-repo11 is shortcut for:

ssh -i  ~/.ssh/id_rsa1 [email protected]

In your case:

Host github.com-repo1
HostName github.com
User git
IdentityFile ~/.ssh/id_repo1
IdentitiesOnly yes

Host github.com-repo2
HostName github.com
User git
IdentityFile ~/.ssh/id_repo2
IdentitiesOnly yes

The URLs would become:

cd /path/to/repo1
git remote set-url github.com-repo1:me/reponame1
git push -u origin main


cd /path/to/repo2
git remote set-url github.com-repo2:me/reponame1
git push -u origin main

In other words, the Host entry is just a name, not an URL.
Use that name as part of your new SSH URL.

CodePudding user response:

This worked for me :

Host github.com-repo1
    User git
    HostName github.com
    IdentityFile ~/.ssh/id_repo1

Host github.com-repo2
    User git
    HostName github.com
    IdentityFile ~/.ssh/id_repo2

Try only those entries, without anything else in ~/.ssh/config

And then :

git clone github.com-repo1:me/reponame1
  • Related