Git can be used to perform multiple operations, including creating, adding, pushing, and cloning the remote repository and branches using Secure Shell Protocol or SSH network, which makes services more secure.
In this tutorial, we will talk about the method of cloning a Git branch with an SSH key in Git.
How to Clone a Branch with SSH Key in Git?
To clone a remote branch with an SSH key in Git, check out the below procedure.
Step 1: Create Git Repository
Open up “GitHub.com” and press the “+” icon to create a new repository:
Next, enter your desired name into the “Repository name” field, choose the “Public” option, which will permit everyone to see it and click on the “Create repository” button:
Step 2: Open Git Bash
Next, launch the “Git Bash” on your system with the help of the “Startup” menu:
Step 3: Generate SSH key
Generate the SSH key-pair (public-private) using the below-provided command:
You will be asked to specify the file in which you want to save the SSH key pair. In our case, we pressed “Enter” and went with the default file:
Step 4: Verify SSH keys
Verify if the keys are generated successfully using the “ls” command:
Below output indicates that SSH keys are generated and saved into the default folder properly. One thing more you should need to know is that here “id_rsa” file contains private and “id_rsa.pub” saved the public key:
Step 5: Launch SSH Agent
Launch the SSH agent with the help of the given command:
As a result, the agent will run as the background process with “1887” as its process id (pid):
Step 6: Add SSH key to SSH Agent
Next, execute the “ssh-add” command to add the SSH key to the SSH agent:
Step 7: Copy Public Key
Copy the generated public key to the clipboard:
Step 8: Open Remote Repository Settings
Open up GitHub, hit the “Profile” icon, and click on the “Settings” option from the open drop-down menu:
Step 9: Add SSH key
Select the “SSH and GPG keys” category from the left-sided column and click on the “New SSH key” button:
Step 10: Add SSH key
Specify the title in the required fields, paste the copied public key in the “key” field, and click on the “Add SSH key” button. Here, we have added “Public_key” as the title:
As you can see, our Public SSH key is added successfully:
Step 11: Verify SSH Connection to GitHub
To verify that the SSH connection is built with GitHub, run the following command:
That’s it! Our GitHub account “GitUser0422” is successfully authenticated using SSH:
Step 12: Copy SSH URL
Next, go to the corresponding repository from which you want to clone the branch, press the “Code” button, and copy the “SSH URL”. In our case, we want to clone the “main” branch of the “Linux_2” repository:
Step 13: Clone Repository
Execute the “git clone” command with the “–branch” option and specify the copied URL to clone the repository:
The “main” branch of the “Linux_2” repository is cloned successfully:
We have compiled the easiest method of cloning a branch with an SSH key in Git.
Conclusion
To clone the remote branch with the SSH key in Git, first, create a new repo on GitHub, open “Git Bash”, and generate the SSH key using the “$ SSH key-gen” command. Launch the SSH agent, then run the “$ ssh-add ~/.ssh/id_rsa” command to add the SSH public key into the SSH agent and copy it. Next, open the repository, copy “SSH URL”, and execute the “$ git clone” command with the copied URL. In this tutorial, we have demonstrated the method of cloning a remote branch with an SSH key in Git.