[Google-oriented Programming(GoP)] Use multiple SSH keys within git commands
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.
Prerequisites
SSH Key
Check out our guide to generating SSH keys or troubleshoot common SSH Problems.
Configuration
Replace
(Recommand)First option, Git configuration
It will configure sshCommand for per git repository.
1 |
|
It will insert sshCommand to [core] segment within .git/config file.
1 | [core] |
Second option, Shell Environment Variable
1 |
|
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.
1 | vi ~/.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:
1 | git clone [email protected]<Your Git Account 1>_github:<Your repository>.git |
SSH Agent
Remember to add SSH Key to SSH Agent to make your SSH private key available for git command.
1 | # Delete all cached keys. |
That’s all done! Now you can use different SSH private keys for multiple git reposities.
References
[2] How to Manage Multiple SSH Keys https://www.freecodecamp.org/news/how-to-manage-multiple-ssh-keys/