Multiple Git Accounts on One Machine With SSH Keys

This blog shows how one can use multiple Git accounts on one development machine. This would become handy if you are working on personal, work, or school projects on the same development machine.  From the development machine, you can pull from and push to remote repositories of different Git accounts, and with the correct users. This blog shows two options but I usually use the first option.

Steps to Using SSH Keys

  1. On the Dev Machine, Generate a new SSH key for each Git Account
  2. Add SSH Key to each Git Account
  3. Create or Update Git Config File
  4. Add SSH Identity
  5. Set up Different Repos using Different SSH Identity

Step 1: Generate a new SSH key for each Git account.

For the latest info, see GitHub’s document generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent 

You can do this by running the following command in your terminal:

# Use ssh-key command to generate a public and private key pair, using 
$ cd ~/.ssh
~/.ssh$ ssh-keygen -t ed25519 -C ""

Then you should see the output:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/jchiang/.ssh/id_ed25519): id_ed25519_jesschiang_git
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519_jesschiang_git
Your public key has been saved in
The key fingerprint is:
The key's randomart image is:
+--[ED25519 256]--+
| |
| o |
| . . + |
| o + * . o |
| = . S = * . |
| o o . X & o |
| o .+ & + . .|
| o.E. + . * = o |
| oo . +o= . |

After running this command, there will be two files generated, id_ed25519_jess and

us147dda0fadf4:.ssh jchiang$ ls
id_ed25519_jess known_hosts

The id_ed25519_jess file contains a private key that should not be shared with anyone.

The contains the public key, which content will be added to the GitHub site to form the trust relationship between the GitHub server and your machine.

Step 2: Add SSH public key to GitHub Account

Add each SSH key to the corresponding Git account. Follow the instructions for adding an SSH key to your Git account.

Follow this doc for instructions: adding-a-new-ssh-key-to-your-github-account 

  1. Go to Git page
  2. At your Git Account page, select the Git Profile icon, and select “Settings” at the drop-down
  3. Follow the instructions to add a new SSH key and select the authentication
  4. Copy and paste id_ed25519_jess content to the New Key content edit field
  5. Save the new SSH key

Step 3: Create or Update the ssh config file

Create a ~/.ssh/config file in your home directory and add the following content. You can leave the user name as git.

Host github-personal
    User git
    IdentityFile ~/.ssh/id_ed25519_jess

# Work account
Host github-work
    User git
    IdentityFile ~/.ssh/id_ed25519_work

Step 4: Add each SSH Key to the SSH Identity List

The block below shows the steps to clear the cache and add the SSH keys

# delete all cached key
$ ssh-add -D

# check your saved keys
$ ssh-add -l

# add each new key 
$ ssh-add ~/.ssh/id_ed25519_jess
$ ssh-add ~/.ssh/id_ed25519_work

Step 5: Set up Local Git Repos w/ Diff Identities

Clone your Git repository using the appropriate Host value in the Git repository URL. For example:

# Personal account
$ git clone git@github-personal:username/repo.git

# Work account
$ git clone git@github-work:username/repo.git