DEV Community

Ali Shaikh
Ali Shaikh

Posted on • Originally published at ali-shaikh.Medium on

Cloning GitHub Repositories with SSH: A Step-by-Step Guide


Photo by Roman Synkevych on Unsplash

In the previous guide, we learnt how to generate SSH Keys on Windows, Linux, and macOS, read here.

In this guide, we’ll explain how to use your SSH keys to clone GitHub repositories — a common task for developers working on projects locally. And remember, these SSH keys aren’t just for cloning; they also secure your push and pull operations.

Prerequisites

Before you begin, make sure:

  • SSH Key Pair: You have generated an SSH key pair, to learn how click here.
  • Git Installed: Git is installed on your system.
  • Internet Access: You can access GitHub.

Adding Your SSH Key to GitHub

Step 1: Copy Your Public Key

Your public key is typically stored in a file like ~/.ssh/id_ed25519.pub (or id_rsa.pub if you’re using RSA). You can simply display the key and copy it.

cat ~/.ssh/id_rsa.pub

OR

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Step 2: Log in to Your GitHub Account

Open your web browser and navigate to GitHub.com. Log in with your credentials.

Step 3: Navigate to SSH Keys Settings

Click your profile icon in the top right corner and select ‘ Settings.’

In the left sidebar, click ‘ SSH and GPG keys.’

Step 4: Add a New SSH Key

Click on the “New SSH key” button.

Title: Enter a descriptive title (e.g., ‘My Laptop Key’).

Key: Paste the copied public key into the key field.

Click “Add SSH key.”

Cloning a Repository Using SSH

Now you are ready to enter the realm of passwordless authentication!

Step 1: Verify Your SSH Connection

Before cloning, verify that your SSH configuration is working:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If it’s your first time connecting, you might be asked to confirm the authenticity of GitHub’s host. Type ‘ yes’ to continue.

If all goes well then you will see the following success message.

This means you are now good to go and use SSH to Clone GitHub repositories.

Step 2: Obtain the Repository’s SSH URL

  1. Navigate to your repository on GitHub.
  2. Click the ‘ Code’ button.
  3. Select the ‘ SSH’ tab and copy the URL

Step 3: Clone the Repository

In your terminal, change to the directory where you want the repository and type ‘git clone’ followed by the ‘ SSH repository URL’ you copied earlier and hit enter.

Aaaand that's it! You have successfully learnt how to clone GitHub repositories without having to enter your username and password.

Conclusion

By following these steps, you’ve not only added your SSH key to GitHub but also learned how to clone repositories securely over SSH.

This method works seamlessly for all Git operations, including pushing new commits and pulling updates from remote repositories. You can follow the same steps to add your SSH keys in GitLab, BitBucket, and more.

This is a very convenient way of accessing Git repositories and will make your development workflows more efficient.

Happy coding!

In a future tutorial, we will learn how to SSH into a remote server.

Top comments (0)