Yesterday, I came across a problem that when I ran the git command
git push origin develop to push some commits to my gitlab repo, it prompted an error:
The project you were looking for could not be found. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
I have two gitlab accounts. For the default one (Acount A) I would like to use
~/.ssh/id_rsa for access. And for the other (Acount B) I use
~/.ssh/id_rsa.b. But it seems my git or ssh client always use
~/.ssh/id_rsa. So, when I wanted to access to my Account B, it will show me the error above.
I went through some posts on the internet, the most common solution is here.
~/.ssh/config file, add
Host gitlab HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes Host gitlab-b HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa.b IdentitiesOnly yes
.git/config file, update remote like this:
[remote "origin"] url = git@gitlab-b:<org>/<repo>.git
This solution didn't work for me. Because I've been using ssh config option
ControlPath to speed up my SSH by reusing connections. But the ssh client can't tell the different connections between
gitlab-b, which will be persisted to a same socket file on my local machine for reuse. So, when I wanted to create a connection to
gitlab-b, it will reuse the connection created by
gitlab. More about speeding up SSH by reusing connections, please read this.
If your git version is newer than 2.12, you may use
core.sshCommand config option for your git repo.
ssh-add ~/.ssh/id_rsa.b git config --add core.sshCommand "ssh -i ~/.ssh/id_rsa.b -F /dev/null"
ssh -T -vv -i ~/.ssh/id_rsa.b firstname.lastname@example.org