Sergio Tapia
Sergio Tapia's Blog

Sergio Tapia's Blog

How to use multiple Github accounts with ssh keys

Subscribe to my newsletter and never miss my upcoming articles

I have my dev box and use it to write both personal projects and work related code.

Here's how to automatically use different ssh keys for different projects on the same machine.


Create two different ssh keys

Make sure you add your public keys to each of your github accounts. Pretty straightforward.

Create a config file in ~/.ssh

Include this:

# My github username is sergiotapia
Host github.com-sergiotapia
    HostName github.com
    User git
    IdentityFile ~/.ssh/mypersonalidentity
        IdentitiesOnly yes        

# Assume my organization is called mycompany
Host github.com-mycompany
    HostName github.com
    User git
    IdentityFile ~/.ssh/myworkidentity
        IdentitiesOnly yes

Configuring your git remotes to properly use the right key

There's two ways for you to go about this.

For new projects you need to clone:

Clone the project using github.com-mycompany or github.com-sergiotapia.

git clone git@github.com-mycompany:mycompany/backend-api.git

# or your personal account

git clone git@github.com-sergiotapia:sergiotapia/backend-api.git

For existing projects you have locally but need to work on.

Set the git remote using the proper naming structure.

git remote remove origin
git remote add origin git@github.com-mycompany:mycompany/backend-api.git

# or your personal account

git remote remove origin
git remote add origin git@github.com-sergiotapia:sergiotapia/backend-api.git
 
Share this