Back to Posts
A close-up, slightly angled view of a computer monitor displaying a web browser. The browser is opened to a GitHub page. The words "Search or jump to…" prompt a user to navigate or search within the site, suggesting an action similar to how one might look up a tutorial on "How to Clone GitHub Repositories Securely with SSH". The out-of-focus elements hint at more content below the address bar.

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

  1. Login to Github, click on your profile, and press settings:

Screenshot_2023-11-17_at_14.28.40.png

  1. Press the option SSH and GPG keys

Screenshot_2023-11-13_at_15.10.18.png

  1. Answer the form

Screenshot_2023-11-13_at_15.11.13.png

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.

Screenshot_2023-11-28_at_14.02.06.png

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) 🥳

Improve your code with my 3-part code diagnosis framework

Watch my free 30 minutes code diagnosis workshop on how to quickly detect problems in your code and review your code more effectively.

When you sign up, you'll get an email from me regularly with additional free content. You can unsubscribe at any time.

Recent posts