How to have multiple GitHub repositories on the same server

in Code Write a comment

As you probably don’t know GitHub doesn’t allow to use the same deploy key for multiple repositories.

If you want to have multiple GitHub deploy keys on the same server the easiest way would be using ssh config aliases.

I wanted to git clone a second GitHub repository on the same server and I got this error message:

➜  git clone [email protected]:Renatello/secondrepository.git
Cloning into 'secondrepository'...
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

GitHub didn’t let me use the same deploy key for a second repository.

Key is already in use

I then came across this solution using ~/.ssh/config. First, generate a second ssh key using ssh-keygen.

➜  ssh-keygen -t rsa -f ~/.ssh/id_rsa_secondrepository
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa_secondrepository.
Your public key has been saved in /root/.ssh/

Start your ssh-agent with the following command:

➜  eval "$(ssh-agent -s)"
Agent pid 31457

And then add your newly generated private key identity to the authentication agent, ssh-agent.

➜  ssh-add ~/.ssh/id_rsa_secondrepository
Identity added: /root/.ssh/id_rsa_secondrepository (/root/.ssh/id_rsa_secondrepository)

Now cat this public key and add it to your second GitHub repository deploy keys.

➜  cat ~/.ssh/

Then, you need to add 2 hosts for GitHub using the key you just created and the key you created previously.

# ~/.ssh/config 

# First host
Host firstrepository
IdentityFile ~/.ssh/id_rsa

# Second host
Host secondrepository
IdentityFile ~/.ssh/id_rsa_secondrepository

You should now be able to clone a second GitHub repository.

➜  git clone [email protected]:Renatello/secondrepository.git
Cloning into 'secondrepository'...

If you find this post useful, please let me know in the comments below.
Renat Galyamov

Want to share this with your friends?

PS: Make sure you check other posts e.g. how to print Javascript object in the console, how to check if a user has scrolled to the bottom in Vue.js or how to do Vue.js polling using setInterval().

Renat Galyamov

Hi, I'm Renat Galyamov

I'm a founder of and MED24.KZ.

A front-end developer with over 9 years of experience building businesses together with founders as well as large enterprises, such as Accenture, The British Museum, Admiral, Kimberley Process, 21st Century Fox and many others.

Subscribe to get my best articles in your inbox first

Join hundreds of weekly readers

I share my coding tips 💻, write about useful services ✌️ and share photos from beautiful places ⛰️

Check if an item already exists in an array in Vue.js/Vuex/ES6 #vuejs #vue #code #tutorial

12:48 PM Apr 9thView tweet

Write a Comment