A quick GitHub SSH clone example
4 Steps to clone GitHub over SSH
To perform a GitHub clone with SSH keys in Git, simply follow these steps:
- Create an SSH keypair on your Windows or Linux OS
- Copy the value of the public SSH key to your GitHub account
- Obtain the GitHub SSH URL for the repository to be cloned
- Using Git, clone from GitHub with the SSH URL
These steps may sound a little daunting at first, but the whole process can be completed in less than five minutes, as you can see from the video above.
Create SSH keys for GitHub
On Windows, you can create GitHub SSH keys with the open source tool PuttyGen. On Ubuntu, or even on Windows if you have the Windows Subsystem for Linux (WSL) service installed, GitHub SSH key creation is even easier. You just invoke the ssh-keygen command in Windows PowerShell or the Ubuntu Terminal window.
[email protected]:~/.ssh$ ssh-keygen -o -t rsa -C “[email protected]”
This command creates a public and private key. These files, especially the private key, must be stored in the user’s .ssh directory for the GitHub SSH clone operation to find them at runtime. Store them anywhere other than the user’s .ssh directory, and your GitHub SSH clone attempt will fail.
|GitHub and SSH everywhere!|
CodeDeploy? BitBucket? GitHub? GitLab? These SSH examples will get you started on every platform:
The following directory listing operation shows the public and private SSH keys in the user’s .ssh directory on Linux:
[email protected]:~/.ssh$ ls id_rsa id_rsa.pub
Copy the public SSH key
To configure SSH with GitHub, you must store a copy of your public SSH key on GitHub.
When you perform a clone of a GitHub repository over SSH, the public key held by the remote server is compared against the private key stored locally in the operating system’s .ssh folder. If there’s a match, the GitHub clone over SSH proceeds. If not, the connection fails. So, for any of this to work, GitHub needs a copy of your public SSH key.
Open your public key, likely named id_rsa.pub, with a text editor and copy the contents. On Ubuntu, you could simply issue a cat command and copy the output from the Terminal window:
[email protected]:~/.ssh$ cat id_rsa.pub ssh-rsa DSSEXaasdf2EEEEAsdaEBgQCwsawea sd9YNasdfaXxkasdfHZgyW7/3WXghBbKasdfKb ewf17c4asdfHQrasdfasPXai6pMsdfsfXQH00L
GitHub SSH config
With the public key copied, log into GitHub and go to your account settings. A link exists for SSH and GPG keys. Click on this link, add a GitHub SSH key, paste the value of the public key into the appropriate field, and give your key a creative name. After this step is performed, you will be able to perform a GitHub clone with SSH keys in Git.
GitHub repo SSH URL
Each GitHub repo has a green Code button you can click to get either an HTTP, CLI or SSH URL that allows the repo to be cloned.
Copy your repository’s SSH URL and you’re ready to perform the GitHub SSH clone operation. The SSH URL for my spock-lizard-docker repository is as follows:
GitHub SSH clone
To clone GitHub with SSH keys, just open Windows PowerShell or an Ubuntu terminal and issue the Git clone command and specify the SSH URL copied from GitHub. Git will ensure that the GitHub clone operation uses an SSH connection.
[email protected]:~/.ssh$ ~$ git clone [email protected]:cameronmcnz/spock-lizard-docker.git Cloning into ‘spock-lizard-docker’… Authenticity of host ‘github.com (188.8.131.52)’ can’t be established. Are you sure you want to continue connecting (y/n/[fingerprint])? y Resolving deltas: 100% (201/201), done. GitHub SSH clone successful.
You’ll encounter a warning about Git’s inability to validate the SSH key against a third party authentication service pauses the clone operation. This is expected. Simply type ‘yes‘ and the GitHub SSH clone operation proceeds.
Once the clone operation is complete, all subsequent interactions with GitHub take place over a secure SSH connection.
And that’s all you have to do to perform a GitHub clone with SSH keys in Git.