How to clone a GitHub repository using SSH for Windows, Linux & MacOS
By: Nintendo Engineer
Tags: Github, Git, Ssh, Software Development, Programming

Photo by Yancy Min on Unsplash

Some people, like me, prefer working with Git using SSH rather than HTTP(S), where you have to enter your Git credentials, which you may have forgotten or it could just be a secure monster of a password that’s not type-friendly.

Whatever the reason, if you’re just starting out in the wonderful world of source control, GitHub or both and want to start using SSH immediately, it can be quite confusing how to go about it. So this is my attempt at making it easy to follow and understand; let’s get to it!

Generating an SSH key

It’s much simpler than it sounds. Pull up a Command Prompt (on Windows) or a Terminal (on Linux/MacOS) at the home folder, which should be it’s default location anyway. Once there, type the following command and hit Enter/Return (you might need to put sudo in front of it on Linux/MacOS):

ssh-keygen

When you do that, you’ll see your OS asking you some questions, starting with where to store the generated public and private key pairs. I leave the default and just hit Enter. This will store the keys in the your-home-folder/.ssh/ folder.

Next you will be asked to enter an optional passphrase. This passphrase is used to encrypt your private key so potential attackers can’t use it even if they get their hands on it. I usually leave this blank, but if you decide to choose a passphrase, make sure it’s one that’s safe and that you will remember (preferably store it somewhere safe). It’s okay if you don’t see anything appear when you type, that’s by design to obscure the length of the password as well.

Next you’ll be asked to repeat the password (or leave empty again if that was your choice) and you’ll get a nice randomart image. Your keys have been created at this point. Let’s go take a look at them. If you’re in your home folder still, it’s easy to get into the .ssh folder, all you have to do is

cd .ssh

and hit Enter. Once here, type dir (Windows) or ls (Linux/MacOS and you’ll see two new files:

The one we’re interested in for use with GitHub is id_rsa.pub.

NEVER EVER share your private key with anyone or anything, that’s for you only. If you think your private key might be compromised, go through these steps again to overwrite your keys.

Now that’s out of the way, let’s copy the contents of this file to your clipboard, so we can use it on GitHub.

For Windows, type the following command and hit enter:

type id_rsa.pub | clip

For MacOS, the command is:

pbcopy < id_rsa.pub

For Linux, I suggest installing xclip:

sudo apt install xclip

and afterwards run:

xclip -sel c < id_rsa.pub

Good! Now we have our key on our clipboard and we can move on to the next section.

Adding your SSH key to GitHub

Log in to your GitHub account and go to your account setting by hitting the icon in the top right and clicking on Settings:

On the left, choose SSH and GPG keys and hit the New SSH key button in the top right

You will see some fields to fill in. Enter a fitting name for your machine and paste what you copied in the Key field:

Hit Add SSH key and you should see it added to your list now.

From now on you can clone repositories with your account using SSH!

Cloning a repository

The easiest part. Just go to any repository you can clone, hit on the Code button, select SSH and copy that URL to your clipboard with the handy button next to it.

Now open a Command Prompt (as administrator) or Terminal (for Windows users I would strongly recommend Git for Windows) at the folder where you want to clone this repository and run the following command:

git clone THE_URL_YOU_JUST_COPIED

and hit Enter.

You should see something similar for the repository you cloned (I used my Twitch Emote Extension repo).

That’s it! You have successfully cloned a GitHub repository using SSH. You can now clone as many more as you like, because the SSH key has now been registered to your account. Merry coding!

Gain Access to Expert View — Subscribe to DDI Intel