Configuring SSH Key for Git

Paridhi Kothari
3 min readJun 15, 2020

SSH, or Secure Shell, is a network protocol that allows one computer to securely connect to another computer over an unsecured network, like the internet.

SSH vs HTTPS

If we use HTTPS, for every action we want to do to our remote repository, it will require you to enter your username and email. It will ask you a lot.

The better alternative is to use SSH key. SSH keys consist of a private and a public key. You will need to keep in mind is that your public key stored on the Git hosting. Then every time we want to do some action to our remote repository, our PC will match the private key (which exists on our PC) with the public key stored in the Git hosting. If they match, then you are allowed to do those action without being prompted the username and password.

Setting up SSH Authentication

  1. Generate the SSH Key on your Computer :

Open your Git Bash terminal and type the below command

$ ssh-keygen -t rsa -C "your_email@youremail.com"

You will be asked for a location to store the key and you will want to just click enter to save it in the default location.

Next it will ask you for a passphrase. This is optional but recommended. Just click enter

2. Add the SSH Key to the SSH Agent

Next we need to let our computer’s SSH agent (the program that distributes and uses the SSH keys) to know which SSH key to use. You probably only have one SSH key on your computer.

We need to enable the SSH agent by writing below command :

eval "$(ssh-agent)"

Now you should have it enabled and you can now set it to use the key we just generated:

ssh-add ~/.ssh/id_rsa

3. Add the SSH Key to GitHub

All that is really needed at this point is to copy the ssh key and paste it into GitHub.

MacOS:

pbcopy < ~/.ssh/id_rsa.pub

Windows:

clip < ~/.ssh/id_rsa.pub

Linux:

You unfortunately don’t have a copy feature built into all distributions of Linux, so if you don’t have one already we will install xClip.

sudo apt-get install xclip

Now that you have it, you can copy it to your clipboard:

xclip -sel clip < ~/.ssh/id_rsa.pub

You can simply paste it into GitHub. Log into GitHub and go to the settings page. You can access the settings page by clicking on your avatar in the top right and going to the bottom of the menu and choosing “Settings”

Select “Add New SSH” and paste your code into the large text field

Hooray! There you go now you can clone your project using SSH link.

I hope this quick overview leaves you with a better understanding of Git using SSH. Hopefully, it’s helpful for you. :)

Please share this link and let me know if you have any questions in below comment box.

--

--