How to accessing multiple Git accounts from the same computer using different private keys?

In order to access multiple github accounts from one computer you need to :

  • generate ssh keys
  • add ssh keys to your github account
  • create a configuration file to manage the separate keys
  • update stored identities

Step 1 - Generating SSH keys:

cd ~/.ssh
ssh-keygen -t rsa -C "[email associated with your github account #1]"
# save it as id_rsa_git_account1 when prompted
ssh-keygen -t rsa -C "[email associated with your github account #2]"
# save it as id_rsa_git_account2 when prompted

As the result of executing these commands, the following files will be generated:

  • id_rsa_git_account1
  • id_rsa_git_account1.pub
  • id_rsa_git_account2
  • id_rsa_git_account2.pub

Step 2 - Adding ssh keys to your github account

Using this command you can copy the key to your clipboard:

pbcopy < ~/.ssh/id_rsa_git_account1.pub

Now as the key is in your account, all you have to do is to open your github account and:

  1. Click on Account Settings
  2. Click “SSH Keys” -> “Add SSH Key”
  3. Paste your key into key field and add a relevant title
  4. Click “Add key” then enter your Github password to confirm.

Step 3 - Create a configuration file to manage the separate keys

  1. Navigate to ~/.ssh/
  2. touch config
  3. Enter the configuration to the config file:
# githubAaccount1 Host account1  
HostName github.com  
User git  
IdentityFile ~/.ssh/id_rsa_git_account1# github Account2  
Host account2  
HostName github.com  
User git  
IdentityFile ~/.ssh/id_rsa_git_account2

Step 4 - Update Stored Identities:

ssh-add -D

Add new keys:

ssh-add id_rsa_git_account1  
ssh-add id_rsa_git_account2

Test to make sure new keys are stored

ssh-add -l

Testing Push / Pull

touch readme.md  
$ git ini  
$ git add .  
$ git commit -am "first commit"  
$ git remote add origin git@account1:githubPersonal/test-personal.git  
$ git push origin master

Publish Date: 2015-04-08