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
- Generate a new ssh key
- Configure ssh on you computer and on each github account
- 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 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.
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