Using SSH keys to clone a repository or branch in Git
SSH Git cloning provides a secure way to clone remote repositories. This tutorial shows the complete method of Git cloning using SSH keys - how to generate SSH keys, set up SSH in Git, and use SSH keys for Git cloning.
We also showed some useful options to extend that functionality - cloning just a branch, cloning to a selected directory, or cloning just a few commits for a large repository.
What is SSH
SSH or Secure Shell Protocol is a network protocol for using secure services over an insecure network. It uses a public-private key pair - your private key is only accessible to you when 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 use SSH keys to clone Git?
SSH Git Clone provides an easy way to securely clone a remote repository over an insecure public network.
If you use SSH keys for Git cloning, you don't have to re-enter your password every time to identify yourself to the remote server. Once the server authenticates the SSH agent, it remembers the details - you don't have to re-enter your SSH key every time.
Generate SSH key pair
We use ssh key-gen
the command to generate an SSH public-private key pair.
ssh key-gen
It will prompt you for a password to protect access to the key on client machines - you can choose to leave it blank, or enter a password of your preference.
Pro Tip: No characters will be displayed as you type your password. This is to hide the length of your password and increase security.
Pro Tip 2: Write down your password somewhere. If you forget it, you will not be able to recover your access.
We leave the command at its default here ssh key-gen
, but we can also pass in different options - the encryption algorithm we want (e.g. ed2559), a label, and a specific location to save the key.
Check the saved SSH key pair
Let's check that our SSH keys were generated and saved correctly.
ls -al ~/.ssh
.ssh
The keys are stored in a folder
on your machine . id_rsa
The file holds your private key, while 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.
Adding SSH keys to the SSH agent
We start the SSH agent as a background process.
eval "$(ssh-agent -s)"
We see that the agent 970
is running as a background process. We add our SSH private key to the SSH agent.
ssh-add ~/.ssh/id_rsa
Identity added : <path_to_private_key
The agent confirms that it has added the private key
with the message .
Add your public key to the remote GitHub account
Copy the public key to your clipboard.
clip < ~/.ssh/id_rsa.pub
Pro Tip: Always copy your KEY. Never share your KEY PUBLIC
with anyone .PRIVATE
-
Go to your
GitHub
Account. -
Click
个人资料图片
. -
Select from the drop-down menu
设置
.
-
Click in the left column
SSH and GPG Keys
.
-
Click in the upper-right corner
New SSH Key
.
-
Add a description and
PUBLIC
paste the key intoKey
the field.
You have now successfully identified your SSH agent with your GitHub account.
Testing the SSH connection to GitHub
We test the SSH connection to GitHub.
ssh -T git@github.com
This message confirms that you have been successfully authenticated.
Using SSH keys to clone a repository in Git
Next, we clone our remote repository using SSH.
- Copy your repository's SSH URL
In GitHub
your repository, click 代码
the green button in the upper right corner.
Click SSH
to display your SSH URL. Copy this SSH URL.
- Git Clone SSH in Git Terminal
Use SSH in the Git terminal to clone the remote.
git clone <remote_repo_ssh_url>
<remote_repo_ssh_url>
is the URL you copied above.
You have successfully cloned a repository in Git using SSH keys.
Extending Git Clone SSH with options
You may need to use a specific type of clone for your use case. There are options available to extend Git cloning over SSH.
- 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>
This is very useful in case of large repositories. To save time and space on your local machine, you may want to clone only the branch you work on or clone only a few branches of interest.
- Use SSH keys to clone Git to a specific location
You may want to clone to a specific folder to keep your local directory structure organized.
To clone to a specific location:
git clone <remote_repo_ssh_url> <specific_location_local>
- Shallow Git clone using SSH keys - clone only a few commits
For large repositories, you may want to reduce cloning time or save local disk space. You can do this by cloning only a selected set of the last few commits.
To clone only the most recent commits:
git clone --depth=<n> <remote_repo_ssh_url>
Here <n>
is the number of the most recent commit you want to clone.
For example, if n = 2
, it will clone only the last two commits.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git installation and establishment of local warehouse service
Publish Date:2025/04/05 Views:89 Category:Git
-
Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper
git remote operation——multiple remote repositories for one project
Publish Date:2025/04/05 Views:131 Category:Git
-
Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓
Git cherry pick command usage
Publish Date:2025/04/05 Views:190 Category:Git
-
git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an
Comparison between Git merge and Git rebase
Publish Date:2025/04/05 Views:171 Category:Git
-
The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th
How to fix Git error Error: src refspec master does not match any
Publish Date:2025/04/05 Views:124 Category:Git
-
When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to
Rebase local branch when pulling changes from remote repository branch in Git
Publish Date:2025/04/05 Views:144 Category:Git
-
This article will cover the basics of rebasing your local branch when pulling changes from a remote repository branch in Git. We use the version control system Git to track changes made to files. We commit changes in a local branch in our l
Undo Git Stash
Publish Date:2025/04/04 Views:187 Category:Git
-
This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash
View a list of cache entries in Git
Publish Date:2025/04/04 Views:59 Category:Git
-
We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T
Git stores specific files
Publish Date:2025/04/04 Views:115 Category:Git
-
This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f