📅 2019-Aug-27 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ git, github, ssh ⬩ 📚 Archive
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.
This problem can be solved by following these steps:
Generate a second SSH key as described here. But instead of overwriting the default ~/.ssh/id_rsa
file, write this key to a separate file. For example: ~/.ssh/second_id_rsa
.
Login to the Github website with the second Github account credentials. Add the second SSH public key ~/.ssh/second_id_rsa.pub
generated in the above step to this account.
Add host entries in ~/.ssh/config
for the main Github host and a second Github host name of your own creation. For example, let us name the second host: github.com-second
. We point the main Github host to the default SSH key file and the second host to the second SSH key file. For example, you might need to add these lines:
# 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, push or pull in repositories from the main Github host should operate normally as before now.
To clone the second repo, use github.com-second
instead of github.com
in the SSH remote URL:
$ 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