אוריאל גימאני
Posted on August 24, 2024
In this short guide ill walk you through setting up multiple GitHub accounts with ssh on the same machine so that different repositories will be automatically related to the right ssh key and account.
Explaining the problem:
Lets say you have two GitHub accounts — one for work, the other for personal usage.
Each of these accounts has one or more repositories on your machine that you want to associate with said account.
Step 1: Setting up ssh
Create an ssh key for each account and add them to your GitHub accounts, You can use GitHub’s guide on how to do that here.
Step 2: Setting up ssh config hosts.
create an ssh config file in your .ssh folder. This file is responsible for telling your ssh agent what key to use for each domain:
touch ~/.ssh/config
Now add the following to the config file:
Host github.com-personal-account # github.com-{name-with-hyphen}
HostName github.com
AddKeysToAgent yes
UseKeychain yes # only if you used a keychain when creating the ssh key
IdentityFile ~/.ssh/private_key_1
Host github.com-work-account
HostName github.com
AddKeysToAgent yes
UseKeychain yes # only if you used a keychain when creating the ssh key
IdentityFile ~/.ssh/private_key_2
You should be able test the success / failure of this part by using the following command:
ssh -T git@github.com-personal-account
# If everything is working you should see this message:
Hi {github account name}! Youve successfully authenticated, but GitHub does not provide shell access.
# if not - troubleshoot the previous steps before moving on :)
Step 3: Associate repositories with the corresponding ssh keys.
There are two different cases we may encounter here;
One is cloning a new repository and the second is attaching an already cloned repository.
Both cases are pretty straightforward so let me explain each of them.
Cloning a new repository:
Go to GitHub and press on the “<>code” button, choose ssh and copy the link.
You’ll receive something like the following: (please follow the example)
git@github.com:{repo-owner-name}/{repo-name}.git
# change it to:
git@github.com-{username-from-host-in-config}:{repo-owner-name}/{repo-name}.git
# example:
git@github.com-personal-account:{repo-owner-name}/{repo-name}.git
#now use it to run git clone:
git clone git@github.com-personal-account:{repo-owner-name}/{repo-name}.git
The commands above will clone the repository and make sure that each git command involving this repository will be directed through the corresponding ssh key.
(Don’t forget to set the user.name
and user.email
properties for your commits metadata).
Linking an existing local repository:
In case you already have a local repository you can set it easily to work with your newly set ssh. Go to the repository base directory and follow these commands:
#this command shows the current origin of the repo
git remote -v
#you will recieve something like this:
origin git@github.com:{repo-owner}/{repo-name}.git (fetch)
origin git@github.com:{repo-owner}/{repo-name}.git (push)
# copy the origin and add to it as follows:
git@github.com-{profile-name}:{repo-owner}/{repo-name}.git
# example:
git@github.com-personal-account:{repo-owner}/{repo-name}.git
# now use the origin you wrote to set the repo new origin
git remote set-url origin git@github.com-personal-account:{repo-owner}/{repo-name}.git
And thats it, you're done.
You can make sure it works by running git pull.
(Don’t forget to set the user.name
and user.email
properties for your commits metadata).
I hope you find this guide helpful , For any questions / corrections / additions feel free to comment below.
Thanks a lot for reading!
Posted on August 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 9, 2024
June 12, 2023