Git

How to Clone a Repo with SSH Key in Git

Cloning is one of the main processes of Git in which we clone remote repositories to create its copy on our system. SSH, known as Secure Shell Protocol, is a network used to secure services over the network. It uses the key pair comprising the public and private keys. This key pair can also be utilized to clone a repository.

This blog will talk about the method of cloning Git repo with SSH key in git.

How to Clone a Repo with SSH Key in Git?

SSH Git clone provides an authentic and secure way to clone remote repositories. To clone a repo with SSH key in Git, follow the below steps:

Step 1: Create Repository
First, open up GitHub hosting service and click on the “+” icon to create a new repository:

Specify the repository name, mark the “Public” option to make it permit everyone to see it and press the “Create repository” button:

Step 2: Open Git Bash
Now, open the “Git Bash” on your system using the “Startup” menu:

Step 3: Generate SSH key
Next, execute the below-provided command to generate the SSH public-private key pair:

$ ssh-keygen

After executing the above command, you will be asked to specify the file in which you want to store the SSH key pair. In our case, we will save it in the default file:

Step 4: Verify SSH keys
Verify if the SSH keys are successfully generated and saved:

$ ls -al ~/.ssh

Here “id_rsa” stores the private key, and “id_rsa.pub” saves the generated public key:

Step 5: Launch SSH Agent
Execute the below-mentioned command to the SSH agent:

$ eval "$(ssh-agent -s)"

Below output indicates that the agent is running as the background process with “1887” pid:

Step 6: Add SSH key to SSH Agent
Now, add the SSH key to the SSH agent with the help of the following command:

$ ssh-add ~/.ssh/id_rsa

Step 7: Copy Public Key
Run the “clip” command to copy the generated public key to the clipboard:

$ clip < ~/.ssh/id_rsa.pub

Step 8: Open Remote Repo Settings
Switch to GitHub, click on the “Profile” icon, and choose “Settings” from the opened drop-down menu:

Step 9: Add SSH key
Next, click on the “SSH and GPG keys” option from the left column and hit the “New SSH key” button:

Step 10: Add SSH key
Add the description in the “Title” field, paste the Public copied key in the “key” field, and press the “Add SSH key” button:

As you can see, our Public SSH key is added successfully:

Step 11: Test SSH Connection to GitHub
Now, to verify the SSH connection is built with GitHub, use the below-mentioned command:

$ ssh -T git@github.com

The below message confirms that we have successfully authenticated:

Step 12: Copy SSH URL
Next, click on the corresponding repository which you want to clone, click on the “Code” button and copy the “SSH URL” to clipboard. In our case, we want to clone the “Cloning_branch” repository:

Step 13: Clone Repository
Execute the “git clone” command with copied URL to clone the repository:

$ git clone git@github.com:GitUser0422/Cloning_branch.git

The given output signifies that the “Cloning_branch” repository is cloned successfully:

We have offered the procedure of cloning a repository with the SSH key in Git.

Conclusion

To clone the repository with the SSH key in Git, first create a new repo on “GitHub”, then open “Git Bash” and generate the SSH key using the “$ SSH key-gen” command. Launch the SSH agent, run the “$ ssh-add ~/.ssh/id_rsa”, add the SSH public key into the agent, and copy it. Next, go to the “GitHub” settings, and add the SSH key. Lastly, open the repository, copy “SSH URL” and execute the “$ git clone” command with copied URL. In this blog, we have illustrated the method of cloning Git repo with SSH key in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.