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/id_rsa_secondrepository.pub.

Hi, I'm Renat 👋

 


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/id_rsa_secondrepository.pub

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 github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa

# Second host
Host secondrepository github.com
HostName github.com
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.
Cheers,
Renat Galyamov

Want to share this with your friends?
👉renatello.com/multiple-github-repos-on-the-same-server

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().

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment

Comment

3 Comments

  1. Thank you for making this so quick and to the point. Looking at the documentation for having multiple repositories on one server was not clear to me. Reading yours and I’m done in 5 minutes.