Configuring multiple github account authentication for different repositories with SSH

Published on May 04, 2021

If you have multiple GitHub accounts like personal and work then it’s important to use the correct account for each repository. Each git client has built in ways to do this with http authentication but this authentication tends to only be set with the specific git client and applies to all repositories you have in the specific application.

It also often means that a different user will be used in shell git compared to your GUI client.

If you want to use the same connection authentication for a git repository from any git client like shell, source tree or the GitHub client then it’s worth setting up ssh for the GitHub account(s) and configuring the repositories to use it.

Why use ssh? - You will always have the same user in every client app you use git with for a repository. You can have multiple github accounts on the same machine and you never have to remember to swap between them.

What we’re going to do

  1. Generate a new ssh key
  2. Configure ssh on you computer and on each github account
  3. Update each git repository to use the correct ssh key

Generating ssh keys

I find it’s easier to do all this stuff directly in the ssh configuration for your account. Assuming your on a Mac thats ~/.ssh. If you’re on another OS check google for the correct location.

Navigate to the directory and run the command below for each GitHub account. Note: not each repository, you just need a cert for each GitHub account. If you just want ssh on one github account then you only need to run keygen once. You could just use one certificate representing your computer here but I personally like to use different certificates per account.

# navigate to the ssh directory
cd ~/.ssh

# Now generate a certificate for each GitHub account you need
ssh-keygen -t ed25519 -C "your_email@example.com"

Key gen will prompt you for a filename. Use something memorable like workaccount-github or personalaccount-github

Key gen will prompt you for a password twice. You’ll need this a couple of times later so remember it or use a password manager.

Repeat for each GitHub account - I have two for example.

Add an entry to your ssh config file

You need to have a config file in the ssh directory so try to open: open ~/.ssh/config

Create the ssh config file if necessary : touch ~/.ssh/config

If you have a config file already and there is a Host * entry already in here you need to comment it out with #'s or delete it. It will override any other settings. This might affect other ssh applications so be careful here.

Now add the configuration to tell ssh that when github.com is used for ssh to use the cert we created. Based on the examples above mine would look like this.

Host github-personalaccount
	HostName github.com
	User git
	IdentityFile ~/.ssh/personalaccount-github
  AddKeysToAgent yes
  PreferredAuthentications publickey
  UseKeychain yes
  IdentitiesOnly yes

Host github-workaccount
	HostName github.com
	User git
	IdentityFile ~/.ssh/workaccount-githhub
  AddKeysToAgent yes
  PreferredAuthentications publickey
  UseKeychain yes
  IdentitiesOnly yes

You will use the Host entry later to tell git clients how to connect to GitHub. This value will replace the “hostname” - github.com in remote origin connections.

The IdentityFile should point to the relevant private certificate you created for that account on GitHub. Private certificate does not have .pub for public at the end.

Add key to agent

Awesome, now add those keys to the Mac ssh agent. To do that run the following command (replace the paths with the private key(s) you just created).

ssh-add -K ~/.ssh/personalaccount-github
ssh-add -K ~/.ssh/workaccount-githhub

Upload the public key to each github account

To get to the right page click on your avater, select Settings at the bottom. Then in the settings screen there is an item “SSH and GPG keys”. Click in there and “New SSH key”.

Give the key a name (I use “macbook” or “main pc” or something like that).

Copy the contents of the public key on your computer using pbcopy.

pbcopy < ./workaccount-github.pub

Make sure you’re copying the right key for the right GitHub account! 😀

Now paste into “Key” on GitHub. The text should look something like

ssh-ed25519 AAAAC3NzaC1lZxxxxxAAAAIM0SlmcGcJrix your_email@example.com

Repeat for any other github accounts.

Test your connections!

Now you have configured the certificates on you computer and on GitHub so we should be able to connect successfully for each one. Use ssh test for this.

# testing i can connect to github as my personal identity
ssh -T git@github-personalaccount

# testing i can connect to github as my personal identity
ssh -T git@github-workaccount

You will get a message like

Hi youexample! You've successfully authenticated, but GitHub does not provide shell access.

This is ok and expected. This means the connection was successful.

Replace the entry in the git repository with the new ssh connection

Almost there! Now for each Git repository we just need to tell it to use ssh and the correct connection we have already set up!

naviagte to your repository and open .git/config in a text editor. You will see a section with a url like this

[remote "origin"]
	url = https://github.com/mypersonalgithub/my-awesome-repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*

We need to change the protocol and host to use the ssh connection we configured. So change this to look like

[remote "origin"]
	url = git@github-personalaccount:mypersonalgithub/my-awesome-repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*

See what changed!? Ok that’s it. Do this for each repository.

Now for my work repository I change the url from https://github.com/ to git@github-workaccount.

Now anywhere I use a git client on my computer I will be using the correct Identity and authentication.


Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia enjoying the mountains and the ocean.

I build and support happy teams that create high quality software for the web.

Contact me on Twitter!


Get new writings, curated tech articles and coding tips!