Issue
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.
Edit2:
This works:
1 set url in each project to corresponding repo:
git remote set-url origin [email protected]:accountName/repo1.git
or
git remote set-url origin [email protected]:accountName/repo2.git
2 .ssh/config:
Host github.com-repo1
HostName github.com
IdentityFile ~/.ssh/id_repo1
Host github.com-repo2
HostName github.com
IdentityFile ~/.ssh/id_repo2
3 push (from each project):
git push -u origin main
Solution
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.
The OP Artur Müller Romanov adds in the comments:
I had to add the repo name (
origin
) to theset-url
command like so:git remote set-url origin [email protected]:accountName/repo1.git.
Then it worked.
Answered By - VonC Answer Checked By - Willingham (WPSolving Volunteer)