How to Clone a Repo or a Branch With SSH Key in Git

Ankit Raj Goyal Feb 02, 2024
  1. What is SSH
  2. Why Git Clone With SSH Key
  3. Use an SSH Key to Clone a Repository in Git
  4. Extend Git Clone SSH with Options
How to Clone a Repo or a Branch With SSH Key in Git

SSH Git Clone provides a secure way to clone remote repositories. This tutorial shows the complete method to Git clone with SSH key - how to generate an SSH key, set up SSH in Git, and Git clone using an SSH key.

We also show some useful options to extend the feature - clone only one branch, clone into a chosen directory, or clone only a few commits for large repositories.

What is SSH

SSH or Secure Shell Protocol is a network protocol to use secured services over an insecure network. It uses a public-private key pair - only you access your private key while you send your public key to the service you want to use.

Any data encrypted with your public key can only be opened with your private key and vice-versa.

Why Git Clone With SSH Key

SSH Git Clone provides an easy way to securely clone remote repositories over insecure public networks.

If you Git clone with an SSH key, you do not have to retype passwords every time to identify yourself to the remote server. Once a server authenticates an SSH agent, it remembers the details - you do not re-enter an SSH key every time.

Generate an SSH Key Pair

We generate an SSH public-private key pair with the ssh key-gen command.

ssh key-gen

generate ssh keys

It will prompt you to enter a passphrase to secure access to your keys on your client machine - you may choose to leave it blank, or enter your favorite passphrase.

Pro Tip: No characters will show when you type the passphrase. This is to hide the length of your passphrase and add more security.

Pro Tip 2: Write your passphrase somewhere. If you forget it, you can not recover your access.

We stay with the defaults for the ssh key-gen command here, but we can also pass in different options- the encryption algorithm we want (e.g., ed2559), a label, a specific location to save the keys.

Check the Saved SSH Key Pair

We check if our SSH keys are generated and saved properly.

ls -al ~/.ssh

check ssh keys

The keys are stored in the .ssh folder in your machine. The id_rsa file holds the private key, while the id_rsa.pub holds your public key.

If you see these two files in the output, you have successfully created and saved an SSH key-pair.

Add the SSH Key to SSH Agent

We launch the SSH agent as a background process.

eval "$(ssh-agent -s)"

launch ssh agent

We see the agent runs as the background process 970. We add the SSH private key to the SSH agent.

ssh-add ~/.ssh/id_rsa

add privatekey sshagent

The agent confirms that it has added the private key with the message Identity added : <path_to_private_key.

Add Public Key to Remote GitHub Account

Copy the public key to the clipboard.

clip < ~/.ssh/id_rsa.pub

Pro Tip: Always copy the PUBLIC key. NEVER share your PRIVATE key with ANYONE.

  1. Go to your GitHub account.
  2. Click on Profile Picture.
  3. Choose Settings from the drop-down menu.

github settings

  1. Click on SSH and GPG Keys in the left column.

select ssh and gpg keys

  1. Click on New SSH Key in the top right.

new ssh key click

  1. Add a description, and paste the PUBLIC key in the Key field.

public sshkey paste to github

You have now successfully identified your SSH agent with your GitHub account.

Test Your SSH Connection to GitHub

We test the SSH connection to GitHub.

ssh -T git@github.com

test ssh connection

The message confirms that you have successfully authenticated.

Use an SSH Key to Clone a Repository in Git

Next, we clone our remote repository with SSH.

  1. Copy the SSH URL of Your Repository

In your repository in GitHub, click on the Code green button in the top right.

Click on SSH to display your SSH URL. Copy this SSH URL.

copy ssh url

  1. Git Clone SSH in the Git Terminal

Clone the remote using SSH in the Git terminal.

git clone <remote_repo_ssh_url>

The <remote_repo_ssh_url> is the URL you copied above.

git clone ssh

You have successfully cloned a repository in Git using SSH keys.

Extend Git Clone SSH with Options

You might want a specific type of clone for your use case. You can extend Git clone via SSH with a few options.

  1. Git Clone SSH Only A Specific Branch

To clone only one branch using SSH keys:

git clone --branch <branch_to_clone> <remote_repo_ssh_url>

git clone specific branch

This is very useful in the case of large repositories. To save time and space on your local machine, you might want to clone only the branch you work on or only a few branches of interest.

  1. Git Clone Using SSH Key Into A Specific Location

You might want to clone into a particular folder to keep your local directory structure well organized.

To clone into a specific location:

git clone <remote_repo_ssh_url> <specific_location_local>

git clone specific location

  1. Shallow Git Clone With SSH Key - Clone Only A Few Commits

You might want to cut down on clone time or save local disk space in case of large repositories. You can do so by only cloning a selected set of the last few commits.

To clone only a few recent commits:

git clone --depth=<n> <remote_repo_ssh_url>

Here <n> is the number of recent commits you want to clone.

For example, if n = 2, it will clone only the last two commits.

git shallow clone ssh

Related Article - Git Clone