Cloning GitHub private repositories using Deploy Keys
Sometimes you want to clone a private repository to a computer without giving it complete access to your GitHub account. While deploy keys allow this, GitHub only allows a particular deploy key to be used in a single repository. This post describes a workaround.
First create a deploy key:
ssh-keygen -t ed25519 -C "deploy key for blahblah"
and save the key as, say, ~/.ssh/key_for_github_blahblah
Add this as a deploy key in the GitHub settings for your repository.1
If you want, you can give this key push access as well.
In ~/.ssh/config
, add an entry like
Host some_unique_name.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key_for_github_blahblah
Now, clone your repository using
git clone git@some_unique_name.github.com:GITHUB_USERNAME/REPOSITORY_NAME.git REPOSITORY_NAME
If your deploy key had write access, changes you make in your local clone can also be pushed to GitHub.