How to create and add SSH key in your Git account

Share this article

We have learned about Git and in the previous article, we have seen why Git is important. As we know that there are different version control systems like GitHub, GitLab, and bitbucket, etc. Adding the SSH key to these platforms is a similar process.

In this article we will learn how to add SSH key in your Git account but the important question arise here why we need to add SSH key in our Git account.

Why do we need SSH keys in Git

  • SSH keys basically identify the user that is working with the Git remote repository.
  • Using username and password of each version control platforms and entering them each and every time when you perform any Git action is hard.

Creating SSH Keys

Generating private and public keys.

We will generate private and public keys in our machine from where we will do SSH to a remote server. Use the following command, it will generate a 2048 bit long key you can change the length of the key by passing the -b {length} option with the commands.

Command Syntax:

ssh-keygen -t rsa

See the following output of the above command.

Verify

After running this command you will see the following files in your user’s home directory.

test@test:~/.ssh$ pwd
/home/test/.ssh
test@test:~/.ssh$
test@test:~/.ssh$
test@test:~/.ssh$ ls
config id_ed25519.pub id_rsa.pub known_hosts.old
id_ed25519 id_rsa known_hosts socket
test@test:~/.ssh$

Add public key on Git lab

Step 1: As already mentioned we are going to use GitLab. Open GitLab and login with your username and password. You will be redirected to your project page. At the same page open click on your profile button located on right top side see the following snapshot and click on Edit profile button highlighted in the snapshot.

Step 2 : After clicking on Edit profile. On left side of the window you will see different options Click on SSH Keys option. For refrence checkout the following image.

Step 3 : In this step, we will add public SSH key which we generated in earlier section “Generating private and public keys”. See the following snapshot for the refrence.

In this step you can also set expiry date of your keys. Then press Add key.

After success you can see the key and its metadata.

Verify the added SSH key

Clone the repository with SSH, for refrence use the Clone with SSH link mentioned in below mentioned snapshot. For more understanding regarding GIT repository you can refer to How to create GIT repository article.

Following snapshot show the output after the clone with SSH

When you do any change in the local repo and you want to commit that work in remote repository. It will not ask you any password. See the snapshot for refrence.

Leave a Comment

Your email address will not be published. Required fields are marked *