← Back to Blog

A guide to setup multiple Github accounts on your Mac

2020-11-28·3 min read
tutorialgit

I find myself doing this everytime I change a new Macbook. Here's how we can use our account simultaneously based on the project we're working on. Example I use here is using work and personal github account.

1. Check for existing SSH keys

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

If there's none, then generate a new SSH key

2. Generate a new SSH Key

First let's create for work account and type this to create SSH key pair for user authentication:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

It will asked where to save the file, press enter because by default SSH keys for user authentication are usually stored in the user's .ssh directory under the home directory. Then, I usually also press enter for the passphrase. After that, It will create public and private key under ~/.ssh/ directory.

> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Now, we will generate another SSH key for our personal account and do the same as above:

$ ssh-keygen -t rsa -C "email@personal.com" -f "id_rsa_personal"

By this time, we have 2 keys generated under ~/.ssh/ directory, which is id_rsa.pub and id_rsa_personal.pub.

3. Add SSH key to ssh-agent

Start agent in background

$ eval "$(ssh-agent -s)"
> Agent pid 59566

check to see if your ~/.ssh/config file exists in the default location.

$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.

If the file doesn't exist, create the file.

$ touch ~/.ssh/config

Open your ~/.ssh/config file, then modify the file, replacing ~/.ssh/id_rsa if you are not using the default location and name for your id_rsa key.

# Work account
Host git.company.com
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa
 
# Personal account
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

4. Add SSH key to the corresponding GitHub account

Copy the SSH key to clipboard

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
  • Login to your GitHub account.
  • In the upper-right corner, click your profile photo, then click Settings.
  • In the user settings sidebar, click SSH and GPG keys.
  • Click New SSH key or Add SSH key.
  • Then, paste your key into the "Key" field.
  • Click, Add SSH key

Do the same for your personal GitHub account. This time, copy ~/.ssh/id_rsa_personal.pub key and add it to your personal GitHub acount.

We're done with the setup. Here's how to use it.

Setup a new project

This is what I do whenever I setup a new project. The key is to set the user name and email used in the project based on the config file.

$ cd myProject
$ git init
 
# setup GitHub username in myProject
$ git config user.name "gitusername"
 
# setup GitHub email in myProject
$ git config user.email "email@personal.com"
 
# add all the file changes
$ git add .
 
# commit the changes
$ git commit -m "initial commit"
$ git branch -M main
 
# make sure the host name is correct based on the config file. example here is @github.com-personal
$ git remote add origin git@github.com-personal:gitusername/my-project.git
 
$ git push -u origin main

Clone from personal github account

$ git clone git@github.com-personal:gitusername/other-project.git

That's it! By using the right host name based on config file, it will take care of the SSH key being used at each of projects that we're working on.

More info about Connecting to Github with SSH.