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. Note, the steps in this article may seem lengthy, but it’s critical for setting up your dev env.

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:

Let’s use ssh-key command to generate a public and private key pair, using

$ cd ~/.ssh
$ ssh-keygen -t ed25519 -C "your.email@gmail.com"
+----[SHA256]-----+

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 id_ed25519_jesschiang_git.pub
The key fingerprint is:
SHA256:n1CMnje7n3UKqw0eGa3N0TjKpUNSmtgzOrPIMh1Yqe0 your.email@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
| |
| o |
| . . + |
| o + * . o |
| = . S = * . |
| o o . X & o |
| o .+ & + . .|
| o.E. + . * = o |
| oo . +o= . |
+----[SHA256]-----+

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

us147dda0fadf4:.ssh jchiang$ ls id_ed25519_jess id_ed25519_jess.pub known_hosts

Files:

  • The id_ed25519_jess file contains a private key that should not be shared with anyone.
  • The id_ed25519_jess.pub 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 

Go to Git page At your Git Account page, select the Git Profile icon, and select “Settings” at the drop-down

Follow the instructions to add a new SSH key and select the authentication

Copy and paste id_ed25519_jess content to the New Key content edit field Save the new SSH key

If you have multiple organizations, make sure you select the one to grant access to

Step 3: 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.

# Personal Account 
Host github-personal
HostName github.com 
User git 
IdentityFile ~/.ssh/id_ed25519_jess

# Work Account  
Host github-work     
HostName github.com     
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