The solution is original from https://superuser.com/a/1519694 , I copied it here for convenience.
Using SSH keys for multiple accounts
If you need to connect to the same host with different keys then you can achieve it by:
Configure the ~/.ssh/config
with different Hosts but same HostNames. Clone your repo using the appropriate host.
Example:
Host work
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_work
User git
Host personal
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_personal
User git
Then instead of cloning repos like:
git clone [email protected]:username/my-work-project.git
git clone [email protected]:username/my-personal-project.git
The host name should be used for cloning:
git clone git@work:username/my-work-project.git
git clone git@personal:username/my-personal-project.git
Document Info
- Author: Paul Tian
- Link: https://paultian.com/2021/02/21/different-ssh-keys/
- Copyright: Attribution-NonCommercial-NoDerivatives (CC 4.0)