How to manage multiple identities for remote git repositories using multiple SSH keys
Time to read: 1 minute
Intro
In this post I will show you how to manage multiple identities targeting a single remote repository provider. I'll be using GitHub in this post but this can most likely be applied to other providers.
Generate SSH key(s)
Go read some documentation
Update ~/.ssh/config
It should look something like this
# main
Host github.com
HostName github.com
IdentityFile ~/.ssh/<main_identity_file>
IdentitiesOnly yes
# alternative
Host alt
HostName github.com
IdentityFile ~/.ssh/<alternative_identity_file>
IdentitiesOnly yes
The idea here is that we define another host which can be used to point to a specific SSH key
Using the new key
Cloning via SSH
Syntax for cloning with SSH looks something like
git clone git@<HOST>:<PATH_TO_REPO>.git
GitHub will give you the following to use
git clone [email protected]:<PATH_TO_REPO>.git
Just replace the github.com with the host that you defined in your config.
So in our case it would be
git clone git@alt:<PATH_TO_REPO>.git
New repo
You would just do the same as above when you define the remote path.
Making commits
Make sure to update your git config to use the email associated with your GitHub account. To get the noreply email you can go to Settings > Emails.