In the previous tutorials about SSH, we have learnt What is SSH Authentication and How to Generate SSH Keys in Git. By now, it is expected that you already have a good understanding of SSH and have generated the keys on your system too. In this tutorial, we will share some thoughts on cloning using SSH in Git. Cloning is one of the main processes of Git. I mean, if you don’t have the code, how will you contribute? Earlier in this course, we came across cloning in Git using HTTPS. Cloning with SSH is almost similar to that with a few hidden twists. But, before cloning with SSH, you must be equipped with some of the pre-requisites:
Pre-Requisites for Cloning Using SSH:
- What is Cloning?
- What is SSH Authentication?
- How to Generate and Add SSH Keys?
One of the main advantages of using SSH for cloning is that you don’t have to enter the password again and again for the hundreds of operation you do in a day. This is actually made possible by a tool or program called ssh-agent. Before starting the cloning process and downloading a repository on our local system, it is better to make you understand about a simple, yet important term called SSH Agent.
What is SSH Agent?
SSH Agent is a helper program for signing in to the clients which, well, require signing in. So why do we need ssh-agent when we know the password? Well, for the same reason that we are interested in SSH apart from its security. Using ssh-agent for our signing purposes will require the user to sign-in only once and the rest of the authentication throughout the session will be handled by the ssh-agent. SSH-Agent provides a secure way to save your passphrase and use them with the client programs. The user need not worry about the security of their passphrase as ssh-agent does not share or hand over these keys while authenticating with the client programs. SSH-Agent opens up a socket over which the client and the user can exchange the signed data. So, there is no need for the user to worry at all.
SSH-Agent comes by default in the Linux based systems and Git Bash of course. So, without any extra work, ssh-agent will be active and come to play once the user opens up the terminal in the Linux based system and Git Bash. But, if you are using some other SSH client to use it over your any other operating system than Linux, you need to follow a few steps.
Setting Up SSH-Agent in The Local System
For setting up SSH-Agent, open your Git Bash in the directory.
Type the following command:
eval “$(ssh-agent -s)”
Execute the command by pressing enter.
Agent xyz will show that the ssh-agent is up and running. The number xyz displayed on the screen in the process id of the process “ssh-agent”.
Adding Keys to SSH Agent
Once we have ssh-agent running, we need to add the keys to the ssh-agent by the following command:
ssh-add ~/.ssh/id_rsa
Execute the command by pressing enter and the keys will be added to your account.
If you have received the same message like mine, your keys are successfully added to the ssh-agent.
Now that we are all set up, let’s clone the repository using ssh.
How To Clone A Remote Repository Using SSH Protocol
Cloning a repository using SSH is very simple, especially if you are already familiar with cloning through the HTTPS protocol. Before cloning, although, you should confirm that you have checked the following steps:
- Have a forked repository in your GitHub account.
- Have Generated the SSH keys.
- Have Added the SSH keys to your GitHub account.
- Have started SSH-Agent (For Non-Linux and Non-Git Bash Users).
- Have added keys to your SSH-Agent (For Non-Linux and Non-Git Bash Users).
The last two steps are optional though.
After checking the above-given steps, navigate to your GitHub account to the repository page which you want to clone.
Press Clone or download and press Use SSH in the panel that appears.
The panel will change to Clone with SSH with the updated link.
Copy the link by pressing the Copy To Clipboard icon.
Open Git Bash and navigate to the directory in which you want to clone the repository.
Check the contents of the repository through ls command.
Type the following command in the Git bash to clone the repository using SSH.
git clone <link>
Cloning into ToolsQA will show that the cloning is in progress.
The following message will appear when the cloning is completed.
Again check the contents of the directory through ls command and notice the repository has been created.
Simple, isn’t it? Cloning is the core part of Git. Since GitHub is made for people to come together with similar interest and contribute towards each other’s project, cloning is what enables this process. Again, as I said earlier, using SSH or HTTPS is solely on the user to use. With this tutorial, we complete the SSH section in GitHub and Git and now will move towards branches in Git.