Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (2024)

Introduction

Secure Shell (SSH) is a cryptographic network protocol that ensures the security and integrity of data transmission over the Internet. In Git, SSH boosts data transmission security and represents a powerful tool for authentication and data exchange. The protocol facilitates a secure and efficient version control workflow.

This tutorial will walk you through setting up SSH and cloning Git repositories using SSH.

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (1)

Prerequisites

  • Git installed (install Git on Ubuntu,macOS,Windows,CentOS 7, orCentOS 8).
  • A remote and local Git repository.
  • An account with administrator privileges.

Set up SSH

To establish an SSH connection, you must create a pair of keys (private and public), share the public key with the service you want to connect to, and set up an SSH agent. The connection requires the user to sign in once, and the SSH agent handles the rest of the authentication throughout the session.

Follow the steps below to set up SSH on Linux or in Git Bash on Windows:

Step 1: Generate SSH Key Pair

The first step is to generate the SSH key pair. Unix systems have a built-in SSH module, while Windows does not. However, it is possible to use the Git Bash terminal on Windows to generate the keys.

Note: If you are on Windows and don't have Git Bash, check out our tutorial for other ways to generate an SSH key.

Open a bash terminal on Linux (Ctrl + Alt + T) or Git Bash on Windows and use the following syntax to generate the SSH keys:

ssh-keygen -t ed25519 -C "[info]"
  • The-tflag allows you to specify the key type. The most commonly used key type for Git is ed25519.
  • The-Cflag is optional and provides additional information about the key, such as its purpose or the creator.

For example:

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (2)

The command prompts you for a location in which to save the files and for a passphrase. To keep the default values, leave everything blank and press Enter to confirm. The command creates a pair of keys whose default file names are id_ed25519 and id_ed25519.pub and saves them in the specified location.

Note: If you have previously created SSH keys, the ssh-keygen command asks if you want to overwrite the existing key. In that case, it is better to create a custom-named SSH key. To do so, specify the -f flag followed by the path to the key with your custom key name.

Step 2: Add Key To GitHub

After generating the key pair, add the public key to the service you want to connect to over SSH. In this tutorial, we will use GitHub.

Follow the steps below:

1. Log in to your GitHub account.

2. In the top right corner, click your account image and select Settings.

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (3)

3. Click theSSH and GPG keyssection and select theNew SSHkey button.

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (4)

4. On the Add new SSH Key page, provide a name for your SSH key. Since you can assign multiple keys to your account, give them descriptive names for easier navigation.Make sure to add the public key you have previously generated, not the private one. Copy the key contents to the designated field.

For example:

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (5)

After adding the key, it should appear in the Authentication Keys section:

Step 3: Test the Connection

After adding a new key, test your connection to make sure everything works as it's supposed to. In the terminal or Git Bash, run the following command:

ssh -T git@github.com
Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (7)

The command output states that you have successfully authenticated. However, in case you get an error saying "permission denied," make sure that you have correctly set up everything. If the error persists, you can delete the keys (locally and in GitHub), generate a new pair, and try again.

Step 4: Check Local SSH Agent

The SSH agent is part of the SSH toolkit. It comes by default in the Unix-like systems and in Git Bash. The SSH agent holds the private key from the key pair we have previously generated.

Before adding the private key to the SSH agent, use the following command to make sure that it is running:

eval "$(ssh-agent -s)"
Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (8)

The output shows that the SSH agent is up and running and displays the process ID.

Note: If you are on Linux, prepend the command with sudo.

Step 5: Add Keys to SSH Agent

The next step is to add the keys to the SSH agent. Use the following syntax:

ssh-add [path-to-private-key]

Specify the entire path to the private key you generated earlier.

For example:

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (9)

The command adds the key to the agent, and you are now ready to clone the repository.

Clone Repository Using SSH Protocol

To clone a Git repository using SSH, use thegit clonecommand followed by a valid SSH URL. The command syntax is:

git clone git@host:username/repository.git
  • hostis the domain name or the IP address of the hosting server. In our case, it is github.com.
  • usernameis your user account on GitHub.
  • repositoryis the name of the Git repository you want to clone.

You can find all the information on your repository page. Follow the steps below:

Step 1: Go to Repository Page

Log in to your account on GitHub and go to the repository page you want to clone.

Step 2: Obtain SSH URL

On the repository page, click the <> Code button to obtain the SSH URL:

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (10)

Make sure to select the SSH option and copy the code snippet.

Step 3: Clone Repository

Paste the SSH URL as an argument to the git clone command in Git Bash. For example:

git clone git@github.com:bosko-pnap/new-repo.git
Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (11)

The command clones the repository to your local machine over SSH.

Cloning with SSH vs. HTTPS

Cloning a Git repository with SSH instead of HTTPS offers advantages in terms of authentication and security. When opting for HTTPS, Git requires you to input your username and password during the authentication process, which can be a potential security risk if not managed carefully.

On the other hand, SSH provides a more secure method for cloning repositories. Instead of transmitting sensitive credentials over the network, SSH uses cryptographic keys for authentication. This approach enhances security and eliminates the need to enter login details repeatedly.

With SSH, only the machines with the corresponding key file can access the repositories, reducing the likelihood of unauthorized access. Even if the SSH key file gets stolen, the potential damage is limited since it doesn't grant access to the entire account. Additionally, you can easily revoke and replace the compromised key, further improving the security of your Git workflow.

Conclusion

This tutorial has outlined the steps required to create an SSH key pair and add it to your GitHub account. The instructions showed how to clone repositories with improved security and reduced reliance on sensitive credentials.

Next, learn the difference between SSH and SSL or see how SSH differs from Telnet.

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step} (2024)

FAQs

Set Up SSH and Clone Repository Using SSH in Git {Step-by-Step}? ›

Navigate to the repository you want to clone. Click on the green button marked "Code." Select SSH from the available options and click the copy button to copy the key to your clipboard. With your Terminal or command prompt open, navigate to the path where you want your Git repo to clone into.

How to clone a git repository with SSH? ›

Navigate to the repository you want to clone. Click on the green button marked "Code." Select SSH from the available options and click the copy button to copy the key to your clipboard. With your Terminal or command prompt open, navigate to the path where you want your Git repo to clone into.

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.

Is SSH key required for git clone? ›

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.

Should I use SSH for Git? ›

Use SSH as a more secure option and HTTPS for basic, password-based Git usage. Since SSH is more secure than entering credentials over HTTPS, it is recommended for businesses dealing with sensitive and critical data. Once you generate the SSH keys, only the machines with the key file on disk can access the repository.

How to clone remote repository in git command? ›

To Git clone a repository navigate to your preferred repository hosting service like GitHub, select the repository you want to clone, copy the repository URL via HTTPS or SSH, type git clone in the command line, paste the URL, and hit enter .

How to create a git repository? ›

To create a new repo, you'll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .git subdirectory in your current working directory. This will also create a new main branch.

Which command is used to clone a repository? ›

git clone is a Git command line utility which is used to target an existing repository and create a clone, or copy of the target repository.

Should I clone with SSH or https? ›

Use SSH as a more secure option and HTTPS for basic, password-based Git usage. Since SSH is more secure than entering credentials over HTTPS, it is recommended for businesses dealing with sensitive and critical data. Once you generate the SSH keys, only the machines with the key file on disk can access the repository.

Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6820

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.