Let’s say you want to give a machine access to one of your repos but
not all of them. This is a reasonable thing to want, and so GitHub
offers deploy
keys. But what if you want to give it access to two repos?
The natural thing to try would be to add the same public key to your
second repo, but GitHub rejects this:
Error: Key is already in use
I think this error is because they don’t want you to get into a
situation where multiple machines are using the same key. If that
happened and you needed to revoke one machine’s access, you’d be
stuck. In this case, however, we only have one machine and we’re
trying to use the same key for two repos. I don’t see any issues with
that setup, and while maybe I’m not being imaginative enough I think
GitHub should probably be checking for duplicate deploy keys on a
per-repo basis instead of globally?
Still, what can we do with GitHub as it is? Generate more keys and
use aliases!
I’m going to walk through this assuming you’re starting from scratch
trying to check out both github.com/you/repo1 and
github.com/you/repo2. If you already have
repo1 working and don’t mind having the two repos
configured differently, just follow the repo2 steps.
First, generate a ssh key for each repo:
$ ssh-keygen -t ed25519 -C “machineName-repo1”
Enter file in which to save the key: /home/you/.ssh/id_ed25519.repo1
…
$ ssh-keygen -t ed25519 -C “machineName-repo2″
Enter file in which to save the key: /home/you/.ssh/id_ed25519.repo2
…
At this point you’ve created the keys and told GitHub to respect them,
but you haven’t told ssh on your machine when to use
which key. You do that in ~/.ssh/config:
Host github-repo1
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519.repo1
IdentitiesOnly yes
Host github-repo2
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519.repo1
IdentitiesOnly yes
The reason this works is that git, like anything else that uses ssh, doesn’t
actually interpret the host name or set up the connection. It just
asks ssh “please connect me to github-repo1”
and ssh will use aliases as part of figuring out how to
do that. This also means that almost any time you might have used
GIT_SSH_COMMAND, or otherwise passed arguments to
ssh, an alias is a better choice.
If you’d already checked out your repo, however, instead of checking
it out again you just change where origin points:
$ cd repo1
$ git remote set-url origin github-repo1:you/repo1.git
Multiple Deploy-Key Repos
Link post
Let’s say you want to give a machine access to one of your repos but not all of them. This is a reasonable thing to want, and so GitHub offers deploy keys. But what if you want to give it access to two repos?
The natural thing to try would be to add the same public key to your second repo, but GitHub rejects this:
I think this error is because they don’t want you to get into a situation where multiple machines are using the same key. If that happened and you needed to revoke one machine’s access, you’d be stuck. In this case, however, we only have one machine and we’re trying to use the same key for two repos. I don’t see any issues with that setup, and while maybe I’m not being imaginative enough I think GitHub should probably be checking for duplicate deploy keys on a per-repo basis instead of globally?
Still, what can we do with GitHub as it is? Generate more keys and use aliases!
I’m going to walk through this assuming you’re starting from scratch trying to check out both
github.com/you/repo1
andgithub.com/you/repo2
. If you already haverepo1
working and don’t mind having the two repos configured differently, just follow therepo2
steps.First, generate a ssh key for each repo:
Then visit
github.com/you/repo1/settings/keys
andgithub.com/you/repo1/settings/keys
and paste the contents of~/.ssh/id_ed25519.repo1
and~/.ssh/id_ed25519.repo2
respectively.At this point you’ve created the keys and told GitHub to respect them, but you haven’t told
ssh
on your machine when to use which key. You do that in~/.ssh/config
:Anyway, now you can check out your repos:
The reason this works is that
git
, like anything else that usesssh
, doesn’t actually interpret the host name or set up the connection. It just asksssh
“please connect me togithub-repo1
” andssh
will use aliases as part of figuring out how to do that. This also means that almost any time you might have usedGIT_SSH_COMMAND
, or otherwise passed arguments tossh
, an alias is a better choice.If you’d already checked out your repo, however, instead of checking it out again you just change where
origin
points:And the same for
repo2
.