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

Published on May 04, 2021

Tagged: #developer-experience

Follow me on twitter for more posts like this

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 "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.

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

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

Host wgh
	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 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 < ./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@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 = 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@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 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.

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

[user]
	name = Darragh O Riordan
	email = my.email.addressg@mail.com

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

git@github.com:darraghoriordan/darragh-oriordan-com.git

We just need to change github.com 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 supporting happy teams that create high quality software for the web.

I also make tools for busy developers. Have you ever spent a week getting your dev environment just right?

My DevShell tooling will save you 30+ hours configuring your dev environment with all the best modern tools. Get it here

https://darraghoriordan.gumroad.com/l/devshell


Read more articles like this one...

List of article summaries

#developer-experience

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!

#developer-experience

How engineers can help deliver software effectively

Delivery managers and team leads have the responsibility to deliver a software system via an engineering team.

Your customer wants every feature to work perfectly and they want it delivered yesterday. Your team wants to learn and grow.

It’s a tough role managing all the stakeholders and creators in a project.

Engineers can help drive great delivery by empathising with and supporting the delivery manager or leads in a project team.

#developer-experience

How to identify a great tech organisation before you join

No one wants to waste time working for an organisation that’s a bad fit. But how do you know a good place from a bad place until you actually work there?

It’s important to spend the same amount of time analysing a potential organisation as they spend analysing you!

During an interview process an organisation has personality testing, resumes, tech testing, behavioural testing, reference checks, bank checks, police checks and more.

If it sounds like a very one-sided process that’s because it is! But there are ways you can bring more balance and here are some important things to research before accepting any engineering job offer.

#developer-experience

Set vscode as the default for text files on mac

How to set the default text editor to be visual studio code on the mac

Update 29 Jan 2022 - I added this and more to comprehensive instructions for configuring a modern developer shell and MAcOS in another article here. You should check that out.