Introduction to Git-Adding an SSH Key

Subscribe to my newsletter and never miss my upcoming articles

Hello everyone!🖤

Before directly diving into the topic I just want to give a small glimpse of ‘what is git basically?’. For this, you need to know about version control. OMG!! Don’t panic. Here is a simple way for you to understand.😉

Version control(VCS) is a software that keeps track of all modifications you make for a particular code. For instance, Rina is working on a project and created a folder which contains all the files/code related to her project. At some point, she thought of making changes to the file and she proceeds. But now, her boss needs the earlier version of code. What to do now?😟. She had already made changes to her file and she hardly remembers where the changes are made.

This is the place where version control comes into play. Version Control records/tracks every change you make to a particular file/folder. So, Rina can travel back to earlier versions easily whenever she needs.

Git

Git is one of a free and open-source distributed version control system, developed to maintain every detail of your project and it is most popular among other VCS’s, as it is comparatively easy to learn. Git even handles large projects with great speed and efficiency.

git-vs-github.PNG

Woah! now you have basic knowledge of this. Let’s look deeper👀. The following concept(Adding SSH Key) will be ignored by many while learning git. So, if you have not done this yet do this along with me (teamwork).😁

Requirements

  • Sign-in to your github account
  • Create a new repository(you can use existing repo if u have one) by clicking new on left side top.
  • Install git to your PC (I’m using version 2.27.0).

Adding an SSH Key

When you are working with github repository using commands, you will often need to identify yourself with username and password always. Instead, you can use SSH key as an alternate way to identify yourself. Therefore, there will be no need to enter the username and password every time.

SSH keys come in pairs, a public key that gets shared with services like GitHub, and a private key that is stored only on your computer. If the keys match, you’re granted access.

  • Open git bash, type the command given below. In place of “” use your mail which is associated with github. This command will create a new SSH using your email as a label.
    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  • After executing the above code, it will show as “Enter a file in which to save the key.” Just click enter so it will take default location(recommended) or you can specify a location.
  • Next, prompt will ask you like this👇. It is just like a password, enter anything you like. The phrase you entered should be remembered.
    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter the same passphrase again: [Type passphrase again]
    
  • At this point, a new key will be generated in the specified location. Add the new ssh key to ssh-agent. The ssh-agent is another program that is part of the SSH tool suite and it is responsible for holding private keys.
  • First, check whether the ssh-agent is running by
    $ eval "$(ssh-agent -s)"
    
  • Now, the following command will add the SSH key to ssh-agent( /.ssh in code is the default folder, you use the location where you have saved the ssh key ). Then, it will ask you to type the passphrase again. Go ahead! It will show as “identity has been added”.
    $ ssh-add ~/.ssh/id-rsa
    
  • Now, copy the public key from the folder and paste it in github (github->settings->ssh and gpg keys->add new key->paste and click “ADD ssh key”).

Screenshot (124).png

That’s it! You have learnt how to add an SSH key. Pat yourself👏

Abhimanyu Sharma's photo

Nice blog.

You can always use this to test your keys + connection with both public and private git repos.

ssh -T git@github.com
Sai Laasya Vabilisetty's photo

Thanks for the info. Glad you read and liked😊

Devsen Datha M's photo

Well explained the article is very informative 💯🙌

Sahil Kazi's photo

Well explained Sai Laasya Vabilisetty

Sai Laasya Vabilisetty's photo

Thank you, Sahil😊