first we need to generate SSH keys in the server.
ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-repo01
ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-repo02
This will generate SSH key in the ~/.ssh, next we need edit SSH config to assign which repository use which key, open ~/.ssh/config.
vi ~/.ssh/config
After open the file, add the following configure.
Host github-repo01
HostName github.com
User git
IdentityFile ~/.ssh/github-repo01
Host github-repo02
HostName github.com
User git
IdentityFile ~/.ssh/github-repo02
Host giftprocessing.github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_giftprocessing
Next is add the public key into the GitHub repository, please link to the repository and click Setting tab in the upper right. Then click Deploy keys → Add new → paste in the Public Key.
Back to the server, if the git repo already exist, we need to update the remote url.
Check current url by:
git remote -v
Set the url to new one:
git remote set-url origin git@giftprocessing.github.com:aad-wsux/giving-day-gift-processing-app.git
Now back to the server, we can use SSH key to access the GitHub, in this case we want to clone repo01 then command will like below.
git clone github-repo01:{UserName}/repo01.git
https://medium.com/@shawnoy/multiple-github-deploy-keys-in-a-single-server-34831d13a919
Your .ssh/config
file points to github.com
and it doesn't know which key to use when it's time to do the pull.
So I found a trick with github.com. You can tell your ssh client that each repository lives in a different github.com subdomain, in these cases, they will be repo1.github.com
and repo2.github.com
So first thing is editing the .git/config
files on your repo clones, so they look like this instead:
For repo1
[remote "origin"]
url = "ssh://git@repo1.github.com/alice/repo1.git"
For repo2
[remote "origin"]
url = "ssh://git@repo2.github.com/alice/repo2.git"
And then, on your .ssh/config
file, now you will be able to enter a configuration for each subdomain :)
Host repo1.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
IdentitiesOnly yes
Host repo2.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
IdentitiesOnly yes
Now you are able to git pull origin master
without entering any passwords from both repositories.
https://gist.github.com/gubatron/d96594d982c5043be6d4