Home

Manage multiple GitHub accounts using SSH keys

In this post I will walk you through my setup how I manage multiple GitHub accounts with SSH keys. You can replicate the process for multiple providers like GitLab and BitBucket also. Let's assume, you have to GitHub accounts: one for work, other for personal purposes.

cd ~/.ssh
ssh-keygen -t rsa -C "YOUR_PERSONAL_EMAIL"
# save it as id_rsa_personal when prompted
ssh-keygen -t rsa -C "YOUR_WORK_EMAIL"
# save it as id_rsa_work when prompted

The above commands setup the following files:

Add the keys to your Github accounts:

Add keys to both github accounts using the commands:

# for unix
pbcopy < ~/.ssh/id_rsa_personal.pub
pbcopy < ~/.ssh/id_rsa_work.pub
# for windows
clip < ~/.ssh/id_rsa_personal.pub
clip < ~/.ssh/id_rsa_work.pub

Create a configuration file to manage the keys separately

create a file named config in the path ~/.ssh/

touch ~/.ssh/config
# or
code ~/.ssh/config

Now paste the following snippet into that file:

# githubPersonal
Host personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# githubWork
Host work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work

Testing the setup

To test our configurations, use the following commands one after the other.

ssh -T personal
# Hi personalUserName! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T work
# Hi workUserName! You've successfully authenticated, but GitHub does not provide shell access.

Usage

You need to take care of the remotes of your local repositories, when you are using this multiple SSH keys setup.

Your need to replace github.com in the remote URL with personal or work

# Original
git@github.com:awsm-page/dev-repo.git

# Modified
git@personal:awsm-page/dev-repo.git

That's it, everything else is normal other than changing the remote URLs.

Don't forget to configure your local username and email addresses correctly before commiting the code. To check your local config, run these commands:

git config user.name
git config user.email



Last Updated on

Next Post: Rules vs Guidelines →

Comments