How to Clone a Repo with SSH Key in Git (2024)

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:

How to Clone a Repo with SSH Key in Git (1)

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

How to Clone a Repo with SSH Key in Git (2)

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

How to Clone a Repo with SSH Key in Git (3)

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:

How to Clone a Repo with SSH Key in Git (4)

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:

How to Clone a Repo with SSH Key in Git (5)

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:

How to Clone a Repo with SSH Key in Git (6)

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

How to Clone a Repo with SSH Key in Git (7)

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

$ clip < ~/.ssh/id_rsa.pub

How to Clone a Repo with SSH Key in Git (8)

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

How to Clone a Repo with SSH Key in Git (9)

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:

How to Clone a Repo with SSH Key in Git (10)

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:

How to Clone a Repo with SSH Key in Git (11)

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

How to Clone a Repo with SSH Key in Git (12)

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:

How to Clone a Repo with SSH Key in Git (13)

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:

How to Clone a Repo with SSH Key in Git (14)

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:

How to Clone a Repo with SSH Key in Git (15)

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.

How to Clone a Repo with SSH Key in Git (2024)

FAQs

How to Clone a Repo with SSH Key in Git? ›

Under setting select SSH and GPG keys, select ssh and new ssh key. Give the key a name, in the box below key paste in the public key you copied from the step above. click add ssh key. Go to the repo you wish to clone and select code, copy the ssh link and return to the terminal in vscode.

How to clone a git repo with ssh key? ›

How to Set Up SSH and Clone Repository Using SSH in Git
  1. Set up SSH. Step 1: Generate SSH Key Pair. Step 2: Add Key To GitHub. Step 3: Test the Connection. Step 4: Check Local SSH Agent. ...
  2. Clone Repository Using SSH Protocol. Step 1: Go to Repository Page. Step 2: Obtain SSH URL. Step 3: Clone Repository.
  3. Cloning with SSH vs. HTTPS.
Nov 2, 2023

How to clone git repository in VS Code using SSH key? ›

Under setting select SSH and GPG keys, select ssh and new ssh key. Give the key a name, in the box below key paste in the public key you copied from the step above. click add ssh key. Go to the repo you wish to clone and select code, copy the ssh link and return to the terminal in vscode.

How to copy SSH key for Git? ›

Do this:
  1. The commands clip < ~/. ssh/id_rsa. pub and pbcopy < ~/. ssh/id_rsa. ...
  2. In the command line, type cat ~/. ssh/id_rsa. pub.
  3. Your key appears! It will be long and full of letters, numbers, and intermittent “+” signs. The prefix of this long key is going to be: ssh-rsa.
Jan 6, 2022

How to use ssh key with git? ›

Steps to setup GitHub SSH connections
  1. Create a GitHub SSH key pair with the ssh-keygen command.
  2. Copy the value of the public SSH key to the clipboard.
  3. Login to GitHub and navigate to your account settings.
  4. Click on the link for SSH and GPG keys.
  5. Click Add Key to register the public SSH key with your account.
Jan 30, 2024

Does git clone need ssh key? ›

In some cases, you might need to use a specific SSH key when cloning a Git repository. This could be due to having multiple keys for different accounts or repositories, or because the key you need to use is not in the default location. Here's how you can specify a particular SSH key when running the git clone command.

How do I manually clone a git repository? ›

  1. On GitHub.com, navigate to the main page of the repository.
  2. Above the list of files, click Code.
  3. To clone and open the repository with GitHub Desktop, click Open with GitHub Desktop.
  4. Follow the prompts in GitHub Desktop to complete the clone.

How to clone a Git repository in terminal? ›

Cloning a repository using the command line
  1. Open “Git Bash” and change the current working directory to the location where you want the cloned directory.
  2. Type “git clone” in the terminal, paste the URL you copied earlier, and press “enter” to create your local clone.

How to use SSH key to clone Git repository Bitbucket? ›

1 Answer
  1. Generate an SSH key: ssh-keygen -t rsa -C "your-email-address"
  2. Press Enter key until a randomart image is generated.
  3. Log into Bitbucket -> View profile -> Manage account -> SSH keys -> Add key.
  4. Paste the key you have generated in Step 1 in the text box. ...
  5. Go to your repository -> Clone .
Oct 30, 2019

How to clone repo with SSH key Bitbucket? ›

To clone your project, click the Clone button on the bitbucket project repository page. Select SSH from the window that opens. You have two options here. You can clone it by typing the code it gives you in your terminal.

How to copy SSH key content? ›

Copy the ssh key into remote servers
  1. Open a terminal on your local computer.
  2. Generate an SSH key pair if you haven't already done so by running the command: ssh-keygen . ...
  3. Once the key pair is generated, run the command: ssh-copy-id user@remote_server . ...
  4. You'll be prompted to enter the password for the remote user account.
Apr 8, 2023

How to use SSH key? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How to copy SSH key using command? ›

Copying Public Keys to Remote Servers
  1. Use the ssh-copy-id command to append the public key in the local ~/.ssh/id_rsa.pub file to the ~/.ssh/authorized_keys file on the remote system, for example: ssh-copy-id remote_user@host.
  2. When prompted, enter the password for the remote system.

What is the Git clone command? ›

The git clone command copies an existing Git repository. This is sort of like SVN checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.

How do I use SSH key in GitHub actions? ›

Usage
  1. Generate a new SSH key with sufficient access privileges. ...
  2. Make sure you don't have a passphrase set on the private key.
  3. Add the public SSH key to the private repository you are pulling from during the Github Action as a 'Deploy Key'.
  4. Add the private SSH key to the repository triggering the Github Action:

How to specify which SSH key to use? ›

To specify which private key should be used for connections to a particular remote host, use a text editor to create a ~/. ssh/config that includes the Host and IdentityFile keywords. Once you save the file, SSH will use the specified private key for future connections to that host.

How to clone repo with ssh key mac? ›

Terminal or an alternative command line tool on your Mac.
  1. Step 1: Generate SSH Key. First, let's generate an SSH key pair. ...
  2. Step 2: Add SSH Key to SSH Agent. Now, let's add the SSH key to the SSH agent on your Mac. ...
  3. Step 3: Add SSH Key to GitHub. ...
  4. Step 4: Clone Repository. ...
  5. Step 5: Verify Cloning.
Apr 2, 2024

How to access a private GitHub repository with a SSH key? ›

Once the SSH key has been added to your GitHub account, you can access your private repository using the repository's SSH address.
  1. In your web browser and while logged in to your GitHub account, go to webpage for the private repository.
  2. Click the <>Code button, then select the Local tab and then the SSH tab.

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5779

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.