Multiple git accounts

October 2, 2017

How to associate different git account to different projects?



Let’s say you have two github accounts, one used at work and the other for your own projects but you want to use both from one linux account. You want to work on each project without a need to type in appropriate credentials everytime you push changes. Firstly you need to generate unique ssh keys for each of them:

ssh-keygen -t rsa -C "your-work-email-address" -f NAME_OF_THE_FILE

For instance:

ssh-keygen -t rsa -C "johnny@companyname.com" -f COMPANYNAME

You should see be asked for enering a passphrase (you can leave it empty) and see something similar to:

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in COMPANYNAME.
Your public key has been saved in COMPANYNAME.pub.
The key fingerprint is:
SHA256:vtpVOppKVx63+NYUkLy11UszAVBbDeL2dIySTkVsFHw johnny@companyname.com
The key's randomart image is:
+---[RSA 2048]----+
|           o+OB=+|
|           .++B*E|
|            **++*|
|           +.+oo |
|        S o + .. |
|       . o * ..  |
|      . o * .o   |
|     . o = o. .  |
|      oo=  ..    |
+----[SHA256]-----+

As a result you should have two files in your current directory: COMPANYNAME and COMPANYNAME.pub.

Now repeat this operation but provide your own details:

ssh-keygen -t rsa -C "johnny@privateemail.com" -f PRIVATE

As a result you should have two new files in your current directory: PRIVATE and PRIVATE.pub.

Copy these four files to ~/.ssh or create this directory first if you haven’t done so yet (~/.ssh should have access rights set to 700)

Having these files there run:

ssh-add ~/.ssh/COMPANYNAME
ssh-add ~/.ssh/PRIVATE

Upload public keys to github

Public keys have pub extension and you should copy the entire content of each file and upload to appropriate github account.

For instance:

  1. Log in to your company’s github account
  2. Go to https://github.com/settings/keys
  3. Click on ‘New SSH key’ button
  4. Give it a title
  5. Paste the entire content of your COMPANYNAME.pub key
  6. Submit the form clicking on ‘Add SSH key’



Repeat these steps for your private github account

Associate different ssh keys with different github account

Let’s assume your github username used at work is companyusername and your own account is johny.

To use both with different ssh keys, you need a config file in your .ssh folder:

vim ~/.ssh/config

#work account
Host github.com-companyusername
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_COMPANYNAME
 IdentitiesOnly yes

#my private account
Host github.com-johny
 HostName github.com
 User git
 IdentityFile ~/.ssh/PRIVATE
 IdentitiesOnly yes

Bitbucket

It also works for bitbucket. Just upload public keys to using bitbucket settings:



and add hosts to ~/.ssh/config:

#work account
Host bitbucket.org-companyusername
 HostName bitbucket.org
 User git
 IdentityFile ~/.ssh/id_rsa_COMPANYNAME
 IdentitiesOnly yes

#my private account
Host bitbucket.org-johny
 HostName bitbucket.org
 User git
 IdentityFile ~/.ssh/PRIVATE
 IdentitiesOnly yes