Deploy 2 GitHub repositories on the same server

This article shows the configuration to deploy 2 GitHub repositories on a single server.


On the Server

1) GitHub does not allow reusing the same deploy key in more than 1 repository. To cater for this restriction, we create a key pair for each repository. 

$ ssh-keygen -t ed25519 -C "For repo1"
$ ssh-keygen -t ed25519 -C "For repo2"

2) Push the private keys into the SSH Agent.

$ eval "$(ssh-agent -s)"
$ ssh-add key1
$ ssh-add key2

3) Create a new config file for SSH.

$ sudo pico /etc/ssh/ssh_config.d/my_repos.conf
                                                                                
Host repo1
HostName github.com
User git
IdentityFile ~/.ssh/key1

Host repo2
HostName github.com
User git
IdentityFile ~/.ssh/key2

4) Reload the SSH process.

$ sudo systemctl restart ssh

On Github

1) Add key1.pub to repo1.

2) Add key2.pub to repo2.


Deployment Script

1) Update the repository URL in your deployment script. For example, in Capistrano:

(New)
set :repo_url, 'git@repo1:username/myrepo1.git'
set :repo_url, 'git@repo2:username/myrepo2.git'

(Old)
set :repo_url, 'git@github.com:username/myrepo1.git'
set :repo_url, 'git@github.com:username/myrepo2.git'

Known Hosts

1) If you haven't been able to connect to the remote before, please remember to add it as your known host. E.g.

ssh -T git@github.com

AI Summary AI Summary
gpt-4.1-2025-04-14 2025-08-04 15:03:32
This article explains how to deploy two separate GitHub repositories on a single server by generating unique SSH key pairs for each, configuring SSH accordingly, updating deployment scripts, and ensuring each key is linked to the correct repository to comply with GitHub's deploy key restrictions.
Chrome On-device AI 2025-08-06 03:00:34

Share Share this Post