Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to setup SSH keys for multiple Github accounts

📅 2019-Aug-27 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ git, github, ssh ⬩ 📚 Archive

Problem

A local Git repository can push and pull from a remote Github account using SSH authentication. This is typically configured by adding your SSH key to your Github account. If you have two or more Github accounts and try to use the same SSH public key for them, Github rejects the key with the error Key is already in use.

Solution

This problem can be solved by following these steps:

# Main Github host
Host github.com
    HostName github.com
    User git
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_rsa

# Second Github host
Host github.com-second
    HostName github.com
    User git
    AddKeysToAgent yes
    IdentityFile ~/.ssh/second_id_rsa
$ git clone git@github.com-second:joe/some_repo.git

This stores the git@github.com-second:joe/some_repo.git URL as the origin in .git/config in the newly created local clone. So all subsequent fetch and push commands will work seamlessly.

For an existing local clone, you can also modify the SSH URL in its .git/config in the same way to obtain the same result.

Tried with: Git 2.17.1 and Ubuntu 18.04


© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email