Configuring different work and personal github accounts on your local repositories with ssh authentication

If you have multiple GitHub accounts like personal and work then it’s important to use the correct account for each repository so you don’t accidentally commit to one with the wrong GitHub account!.

Each git client has built in ways to do this with http authentication but http authentication tends to only be set with the specific git client and applies to all repositories you have in the specific application.

Using http authentication 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 ""

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 is used for ssh to use the cert we created. Based on the examples above mine would look like this.

I use host “pgh” as shorthand for personal github and “wgh” as shorthand for work github

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

Host wgh
	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” - 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 avatar, 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 < ./

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

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@pgh

# testing I can connect to github as my personal identity
ssh -T git@wgh

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 =
	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@pgh:mypersonalgithub/my-awesome-repo.git
	fetch = +refs/heads/*:refs/remotes/origin/*

For work account you would use url = git@wgh:..........

See what changed in the url property!? Ok that’s it. Do this for each repository you want.

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

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

Set the github user

You should also set the github user per repository or git will use the global value. To set the user add a section to your git configuration

	name = Darragh O Riordan
	email =

Cloning github repositories in the future

On github click the Clone button. Select ssh instead of https and copy that url. It will look something like this

We just need to change to the custom host we want to use and clone it. e.g.

git clone git@wgh:darraghoriordan/darragh-oriordan-com.git

Enjoy working on personal projects and work projects without accidentally committing to one with the wrong GitHub account!

Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia building and supporting happy teams that create high quality software for the web.

I also make tools for busy developers! Do you have a new M1 Mac to setup? Have you ever spent a week getting your dev environment just right?

My Universal DevShell tooling will save you 30+ hours of configuring your Windows or Mac dev environment with all the best, modern shell and dev tools.

Get DevShell here: ✨

Read more articles like this one...

List of article summaries


Start tracking DORA metrics for your team in just 15 minutes with Apache Dev Lake

DORA (DevOps Research and Assessment) metrics are an excellent way for engineering organisations to measure and improve their performance.

Up until now, monitoring the DORA metrics across Github, Jira, Azure Devops etc required custom tooling or a tedious manual process.

With Apache Dev Lake you can get beautiful reporting for DORA metrics on your local machine in as little as 15 minutes (honestly!).

From Google Sheets to Grafana
From Google Sheets to Grafana


How to use SSH with Git and ssh-agent on Windows

I needed to run git natively in windows (no wsl) for a recent project. I use ssh certificates with passphrases to authenticate with my git provider.

Ssh requires the certificate passphrase every time you use a connection. It’s annoying typing this passphrase in to terminal when using a git command.


How to fix custom type definitions not being type checked

If you have a custom types file (e.g. myTypes.d.ts) but you get no errors from the type checker and compiler in typescript even though you know there are issues in the file you might have skipLibCheck turned on.


Consistent modern shell tooling on MacOS and Windows WSL for developers

I regularly code on both MacOS and Windows machines and I was always annoyed how different the default experiences are on each. I need to use the same tools and the same experience on both.

Windows “WSL” (Windows Subsystem for Linux) is a great tool for this you can use on Windows 10 and newer. The latest version lets you run a full Ubuntu instance that integrates seamlessly with the underlying windows instance.

By using WSL2 you can have a (mostly) identical developer experience jumping between MacOS and Windows.

Better tooling for Developers

Many of the terminal tools that come with unix environments are functionally similar to how they were 20 years ago. But other developer tooling has advanced quite a bit since then.

You can replace tools like ls or cat with modern equivalents that support full colour, unicode icons, git state and more. Terminal prompts can be made git aware and use colour to indicate state so you don’t have to query git so often.

Keeping developer experience consistent across machines

Keeping any shell changes you make on one machine up to date on all the machines you code on is a nightmare without the right tooling.

This article also explains all the tools I use and how I keep the same terminal setup consistent on MacOS and Windows!

Let’s go!