Setting up Git with SSH.

Vijay Tembugade
8 min readAug 8, 2021

So, if you are a fresher, and recently joined any of the organization as a developer , then the first thing they would ask you to do is learn git and set up your environment with git. And Mostly they will ask you to set up this with gitbash and ssh. This is one of the scenarios, but when it comes to ssh, it adds a lot of things like connecting a remote desktop, virtual machines, and also in a lot of cloud instances. But we have to start by setting up our git with ssh.

SSH

SSH stands for secure socket shell protocol, where, we know what is ‘secure’ which means with the fixed use case and in proper way without any illegal activity involved in it. ‘Protocol’ means, it is a kind of rule. “Shell” is something that we need to set up which is exactly in our terminal(Linux) or command prompt (windows).

Most common use cases of SSH:
1. Accessing Github/gitlab etc. services.
2. Remotely accesing another computers
3. Accessing servers or cloud instances.

To establish ssh connection we need two primary components, i.e. Client and Host. Here, we are setting up the out local repository with GitHub repo, so, the client will be our system and the Host will be Github.

Before, we start to understand the connection between our local machine and Github, first understand the components in SSH. How does the ssh works? SSH has three types of Encryption. Symmetric Encryption, Asymmetric Encryption, and Hashing. Each encryption has keys, in some cases, we have a public key and private key and in some cases, we have both the keys.

Symmetric Encryption has keys on both sides, i.e. on the client-side and the server-side. So, a connection between those can be established if both of them have proper keys. There can be a case that anyone with the key can access the connection. Asymmetric Encryption has two types of keys, private key, and public key. The private key is stored at the client side for security purposes and a public key is shared. Here, the public key is works as Encryption and the private key is works as decryption. So, the client can connect with services that have public keys and the connection will be secure. Hashing is a form of cryptography, where hash bases message authentication code(HMAC) is used.

In Asymmetric Encryption, Diffie–Hellman key exchange algorithm is used. You can know more about it by clicking the link or reading mentioned Wikipedia article.

Now, lets see how does SSH comes into our daily practise?

Let’s start with setting up ssh and creating ssh keys in our system. First of all download git on your system. Install it on your system. For windows, we can install it, and then gitbash will be available to us. But for Linux git is already on the terminal if it is not present check out this link.

Open the gitbash in windows or for linux open the terminal and type following command.

ssh-keygen -C "username@email.com"

If this command has not worked on your Linux system see this link. And if you don’t see any problem you will get the something like one shown below. Remember -C represents the comment and after that, we have to put our email address in double quote.

Here you can see, it is generating public and private RSA key pairs. It is asking where to save those keys, press Enter, as it will save it on the default location.

(If you want to know more about RSA, click here.)

Next step it will ask you for passphrase, if you are beginner keep it blank and press Enter.

It will show something like this, where it will have location of keys are stored and fingurprint of the key that are generated.

Now type following command to move into .shh folder, which is located into your system.

cd ~/.ssh
ls

You will see something like this.

id_rsa is the private key generated in your system and id_rs.pub is the public key generated for your system. Now we have to put that id_rsa.pub into our GitHub account and then we can create ssh connection between our gitbash terminal and Github.

Now, type the following command and copy the key which is shown on the terminal. Remember we have to copy the key .pub extension on it, which is the public key.

cat id_rsa.pub

Now it will show the public key as shown in following firgure and copy that key.

Now open your github account and go to Settings.

Now , you will see the SSH and GPG keys section there and click on it and you will see the New SSH key button.

Now click on New SSH Key and you will see the title and Key Section there, Give the title that you want and paste the key that you have copied from gitbash terminal.

And click to Add SSH key. It might ask you for github password , give that password and proceed ahead.

Now , our next step is to let our PC know that we are using SSH for connecting with remote repo or services. So, we need to add our private key i.e. id_rsa to our system , so that it will know , which key to choose while connecting with github. So, get back you gitbash terminal and type,

eval $(ssh-agent -s)

It show the process id and agents of SSH. Now add our private key to our system configuration.

ssh-add ~/.ssh/id_rsa

It will add out identity into our system configurations. Next time whenver we change our system’s ssh keys then we must add those to our system by running above two commands.

Now, it is important to know that we should connect out github account’s username and email with our gitbash and for that we need to set them up. For that in gitbash terminal type following command one by one.

git config --global user.name "github_username"
git config --global user.email "github_email"

for github user_name and github_email you have to mention your username and email associated with your github account.
Now type following command and you will set up a connection with your gitbash terminal and github.

ssh -T git@github.com

It will say that your account is successfully connected with github.

Or else it will ask to Are you sure and you want to connect? Something like below Image. For that Type ‘yes’ and press Enter.

If it gives any error then google that error and you might get some proper solution on that.

Now, restart your gitbash by closing and reopning it. Lets clone any repo from github with ssh connection. Go to your github account and copy the ssh cloning url for your repo.

After copying to to gitbash terminal and type

git clone git@github.com:github_username/repo_name.git

and after presing Enter you will see something like this. Type “yes ” and again press Enter. If it doesn’t show any of this prompt then ignore and it will clone your repo securely on your PC.

Congratulation! You have successfully and securely set up your git connection with github in ssh.

Although, you might have doubt that , if we have HTTPS connection from github , why we are using SSH?

  1. By using HTTPS , we need to login everytime while pushing code. But by SSH , it get directly connected. This is one of the major reason lot of them prefer SSH over HTTPS.
  2. Lot of organisation prefer to make their github repo private, so, it become easy to use SSH to connect private repos as per security concern.

Some other learning resourses for git and github:

  1. Youtube video from freeCodecamp
  2. GitHub Documentation
  3. Cheatsheet

Some Important things that you should know:

  1. Gitbash doesn’t work ctrl+c and ctrl+v command , for that it has separate command / shortcuts , you need to check them out here . Or , you can use mouse by right clicking. For gitbash to paste, we have to use shift+insert.
  2. Some other things to know about ssh-add, i.e. to remove identity/key from windows of ssh-key . i.e. unlink your windows configuration with SSH.
ssh-add -D

3. To list down all the keys present in your system

ssh-add -l

4. You can cross check whether your email and username is added or not by commanding following in your terminal/gitbash.

git config --list

Thank you for reading!

If you like the blog, feel free to connect with me on Twitter and LinkedIn.

“It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.”

— Jack William Bell

--

--