Clone a private repo on a computer with multiple GitHub accounts

gitgithubssh

I'd like to be able to use different GitHub accounts for different repos on my computer.

But when I try cloning a private repo from the 2nd account, I get this error:

$ git clone git@githubpersonal:2nd-github-account-username/redacted.git

Cloning into 'redacted'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

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

In ~/.ssh/config I have the SSH key for my second GitHub account set up, which is id_rsa_personal. id_rsa is the one the other GitHub account uses, and it works.

Host githubpersonal
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

What I've tried so far

At first I thought it might be an issue with the user.name and user.email in my ~/.gitconfig not matching the account. So I added

[includeIf "gitdir:~/projects/"]
    path = ~/projects/.gitconfig

…and made a ~/projects/.gitconfig with the other name & email. That did not work.

Then I read that you need to be in an existing git repository for that includeIf to work, which doesn't make sense before you clone the repo, so I just switched my global gitconfig:

$ git config --global user.name "<2nd-github-account-username>"
$ git config --global user.email "<2nd-github-account-email>@gmail.com"

Still no luck. git config --get user.email and user.name show the expected values.

I'm not sure where else to look from here.

Best Answer

If the specified keypair is not being honored, try enabling the IdentitiesOnly option. The client might be using the wrong key from ssh-agent.

Test using ssh githubpersonal – make sure the server greets you with the expected GitHub account name.

The user.* settings are not used for authentication. The Git protocol has no authentication of its own, only the one provided by SSH (or HTTPS).

Related Question