In the previous tutorials about SSH, we have learned What SSH Authentication is and How to Generate SSH Keys in Git. By now, we hope that you have a good understanding of SSH and have generated the SSH keys on your system.
In this tutorial, we will share some thoughts on cloning a remote GitHub repository using SSH. 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:
- What is Cloning?
- What is SSH Authentication?
- How to Generate and Add SSH Keys? (This chapter)
One of the main advantages of using SSH for cloning is that you don't have to enter the password multiple times for the hundreds of operations you do in a day. It is 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 a simple yet relevant term called SSH Agent.
What is SSH Agent?
SSH Agent is a helper program for signing in to the clients, which, well, requires signing in. So why do we need an 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 ssh-agent will handle the rest of the authentication throughout the session. 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 begin 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 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 is 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 add to your account.
If you have received the same message as above, it implies the successful addition of your keys to the ssh-agent.
Now that we are all set up let's clone the repository using ssh.
How To Clone 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 completes.
Again check the contents of the directory through ls command and notice the creation of the repository.
Simple, isn't it? Cloning is the core part of Git. Since GitHub brings people to come together with similar interests and contribute to each other's projects, cloning is what enables this process. Again, as I said earlier, using SSH or HTTPS is solely on the individual to use. With this tutorial, we complete the SSH section in GitHub and Git and now will move towards branches in Git.