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:
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, push or pull in repositories from the second Github account, use
github.com-second instead of
github.com in the SSH remote URL. For example:
firstname.lastname@example.org:joe/some_repo.git. You can also modify the
url field in the
.git/config file of these repositories.
Tried with: Git 2.17.1 and Ubuntu 18.04