Git

How to Clone a Branch with SSH Key in Git

People prefer the Git version control system for tracking their team projects. They make copies of their work on local directories and save the original on a remote repository. It is feasible for everyone to understand and use this platform because of multiple simple features.

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:

$ ssh-keygen

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:

$ ls -al ~/.ssh

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:

$ eval "$(ssh-agent -s)"

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:

$ ssh-add ~/.ssh/id_rsa

Step 7: Copy Public Key
Copy the generated public key to the clipboard:

$ clip < ~/.ssh/id_rsa.pub

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:

$ ssh -T git@github.com

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:

$ git clone --branch main git@github.com:GitUser0422/Linux_2.git

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.