I have 2 Github accounts that I want to use. It becomes problematic when you want to use one, but git uses the incorrect SSH key for access.

The solution is to modify your SSH config with the options you want, and to configure your git repo to use the right SSH host.

Generate SSH keys

Each key is meant to be used on a single GitHub account.

# Each command will prompt for a passphrase.  Feel free to leave them empty and just hit <enter> to continue, but it's most secure if you enter one.

# -a: rounds, default: 16.  100 rounds is more secure than 16 rounds
# -t: algorithm to use. ed25519 is recommended over all other algorithms
# -f: location of the private key
# -C: comment added
ssh-keygen -a 100 -t ed25519 -f ~/.ssh/my_1st_github_account -C "first@example.com"
ssh-keygen -a 100 -t ed25519 -f ~/.ssh/my_2nd_github_account -C "second@example.com"

Grant each access to their respective GitHub accounts

Under https://github.com/settings/keys for each account, upload your corresponding public key.

  • For my_1st_github_account, you can cat ~/.ssh/my_1st_github_account.pub. Copy and add it into the keys for your 1st account.
  • For my_2nd_github_account, you can cat ~/.ssh/my_2nd_github_account.pub. Copy and add it into the keys for your 2nd account.

Configure SSH

# ~/.ssh/config
Host ssh_one
UseKeychain yes
AddKeysToAgent yes
Hostname github.com
User git
IdentityFile ~/.ssh/my_1st_github_account
IdentitiesOnly yes

Host ssh_two
UseKeychain yes
AddKeysToAgent yes
Hostname github.com
User git
IdentityFile ~/.ssh/my_2nd_github_account
IdentitiesOnly yes

Configure the repos’ git config to use the specific SSH configs

When we’re inside the repo and try to push or pull, we’ll use the specific SSH settings to do so.

Repo that uses ssh_one to connect to my_1st_github_account

Notice that url doesn’t use .git, but pushurl does.

# myrepo/.git/config
[core]
        ...
[remote "origin"]
        url = ssh_one:my_1st_github_account/myrepo
        ...
        pushurl = ssh_one:my_1st_github_account/myrepo.git
[user]
        name = First
        email = first@example.com

Repo that uses ssh_two to connect to my_2nd_github_account

# another/.git/config
[core]
        ...
[remote "origin"]
        url = ssh_two:my_2nd_github_account/another
        ...
        pushurl = ssh_two:my_2nd_github_account/another.git
[user]
        name = Second
        email = second@example.com