How to Clone GitHub Repositories Securely with SSH
By Andreas Bergman
Cloning a GitHub repository is a fundamental skill, and doing it with SSH adds an extra layer of security. In this post, I’ll walk you through the process of cloning a repository that contains all video examples from the YouTube channel ArjanCodes using SSH. But don’t worry, this guide works for any repository that you have access to, private and public!
If you’re in need of a refresher on key generation, I’ve also got you covered. Check out this post.
Add the ssh key to your Github account
- Login to Github, click on your profile, and press
settings
:
- Press the option
SSH and GPG keys
- Answer the form
Give the SSH key a good name that references where the connection will come from, for example, private_macbook_air
. We do this because we might have multiple devices that want access through SSH and if one key gets compromised (knock on wood), we’ll know which one to remove.
Keep Key type
the same, as you’ll use the key for authentication.
Paste the content of the PUBLIC SSH key into the key field, and then press add SSH key.
Now, we’ll be able to choose the option to use SSH when cloning remote repositories from GitHub.
Setup ssh key usage for the git clone
command
Just one last configuration step, and then we’re done! Before you can clone the repository, we need to configure our SSH config file to use the correct key used when cloning; otherwise, this won’t work. 😞
If you don’t have a config file, create one with the following command:
touch ~/.ssh/config
Let’s edit the config
file with our favorite text editor (which, of course, is vscode 😉) and add the following:
Host github.com
IdentityFile ~/.ssh/NAME_OF_KEY
Now, when everything is setup, navigate to the remote repository on GitHub and choose SSH and copy the command.
Navigate to a folder locally where you can keep your repositories. In this case we use a directory called ./repos
that is located in root. If not, it’s totally fine! Just don’t do it on desktop; it gets ugly real quick. As developers, we are pedantic when it comes to structuring our computers, but not our homes. 🥸
mkdir ~/repos
Navigate to the wanted directory where you want to clone.
And then, finally, we’ll do what we’ve waited for.
git clone git@github.com:ArjanCodes/examples.git
With the setup we’ve done now, we can easily clone repositories and review code from GitHub.
Congratulations! We now have a local version of the ArjanCodes code examples with all material related to the videos (not the actual videos) 🥳