Github or Gitlab use ~/.ssh/id_rsa as the default SSH private key. But sometimes you need more accounts than one for access to Github or Gitlab and similar tools. For example you can have one account for your projects at home and second account for your company.
There are three ways to Use multiple SSH private keys within git commands.
Check out our guide to generating SSH keys or troubleshoot common SSH Problems.
(Recommand)First option, Git configuration
It will configure sshCommand for per git repository.
It will insert sshCommand to [core] segment within .git/config file.
Second option, Shell Environment Variable
Can append GIT_SSH_COMMAND environment variable to your shell bash. Such as ~/.zshrc, ~/.bashrc, etc.
Third option, SSH configuration
Assign different Hosts for multiple host reposities within ~/.ssh/config.
Must use <Your Git Account 1>_github or <Your Git Account 2>_github instead of git.com while use git command. Such as:
git clone git@<Your Git Account 1>_github:<Your repository>.git
Remember to add SSH Key to SSH Agent to make your SSH private key available for git command.
# Delete all cached keys.
That’s all done! Now you can use different SSH private keys for multiple git reposities.