close
close

first Drop

Com TW NOw News 2024

Setting up multiple ssh keys and git repositories on one machine
news

Setting up multiple ssh keys and git repositories on one machine

In this short tutorial, I’ll walk you through setting up multiple GitHub accounts with ssh on the same machine, so that different repositories are automatically associated with the correct ssh key and account.

Explaining the problem:
Let’s say you have two GitHub accounts: one for work and one for personal use.
Each of these accounts has one or more storage locations on your computer that you want to associate with the account.

Step 1: Set up SSH

Create an SSH key for each account and add it to your GitHub accounts. You can use GitHub’s guide to see how to do that.

Step 2: Set up SSH configuration hosts.

create an ssh configuration file in your .ssh directory. This file is responsible for telling your ssh agent which key to use for each domain:

touch ~/.ssh/config
Go to full screen mode

Exit full screen

Now add the following to the configuration 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
Go to full screen mode

Exit full screen

You can test the success/failure of this component using the following command:

ssh -T [email protected]

# 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 :)
Go to full screen mode

Exit full screen

Step 3: Associate repositories with their corresponding ssh keys.

We can encounter two different cases here;
The first is cloning a new repository and the second is linking an already cloned repository.
Both cases are quite simple, so I’ll explain them both.

To clone a new repository:

Go to GitHub and press the “code” button, choose ssh and copy the link.
You will then receive something like the following: (follow the example)

[email protected]:{repo-owner-name}/{repo-name}.git

# change it to:
[email protected]{username-from-host-in-config}:{repo-owner-name}/{repo-name}.git

# example:
[email protected]:{repo-owner-name}/{repo-name}.git

#now use it to run git clone:
git clone [email protected]:{repo-owner-name}/{repo-name}.git
Go to full screen mode

Exit full screen

The above commands will clone the repository and ensure that any git commands related to this repository are routed through the corresponding ssh key.

(Don’t forget to user.name And user.email properties for your commit metadata).

To link an existing local repository:

If you already have a local repository, you can easily set it up to work with your newly set up ssh. Change to the repository’s home directory and follow these commands:

#this command shows the current origin of the repo
git remote -v

#you will recieve something like this:
origin [email protected]:{repo-owner}/{repo-name}.git (fetch)
origin [email protected]:{repo-owner}/{repo-name}.git (push)

# copy the origin and add to it as follows:
[email protected]{profile-name}:{repo-owner}/{repo-name}.git

# example:
[email protected]:{repo-owner}/{repo-name}.git

# now use the origin you wrote to set the repo new origin 
git remote set-url origin [email protected]:{repo-owner}/{repo-name}.git
Go to full screen mode

Exit full screen

And that’s it, you’re done.

You can check if it works by running git pull .
(Don’t forget to user.name And user.email properties for your commit metadata).

I hope you find this guide useful. If you have any questions, corrections or additions, please leave a comment below.
Thank you very much for reading!