| by Arround The Web | No comments

How Can I Add an Already Generated SSH Key to Git Bash?

While performing the push and pull operation on Git projects, it is required to connect the local repository with the remote repository. Git provides two methods for cloning remote repositories, such as HTTP protocol and SSH protocol. The HTTP protocol is the commonly used method for tracking project changes. However, it is not secure for sensitive development projects.

On the other hand, the SSH protocol is the most secure method that uses a pair of public and private keys. To communicate between the local repository and the remote repository, developers first need to generate the SSH key and add it to the Git bash.

This blog will demonstrate:

How to Generate the SSH Key?

To generate the new SSH key, try the following steps:

  • Navigate to the required directory.
  • Execute the “ssh-keygen” command to create the SSH key
  • Verify the generated key.

Let’s move ahead and implement the above-stated procedure for a better understanding!

Step 1: Switch to Desired Repository

First, type out the following command in the terminal and redirect to the Git repository:

$ cd "C:\Git"

Step 2: Generate SSH Key

Then, create an SSH key with the help of the “ssh-keygen” command:

$ ssh-keygen

After executing the above command, it will ask you to specify the repository where you want to create the SSH key. However, we have already created the SSH key. That’s why the existing key has been over-written. Press the “y” key and complete the process:

Step 3: Verify SSH Key

Utilize the below-stated command to ensure whether the SSH key has been generated:

$ ls -al ~/.ssh

As you can see in the following output, the SSH has been generated successfully. Here, “id_rsa.pub” keeps the public key, and “id_rsa” saves the private key:

How to Add an Already Generated SSH Key to Git Bash?

First, launch the SSH agent to add an already created SSH key to Git bash. Then, utilize the “ssh-add ~/.ssh/id_rsa” command.

Step 1: Start SSH Agent

Write out the following command to launch the SSH agent:

$ eval "$(ssh-agent -s)"

The below screenshot indicates that the SSH agent is running in the background with “3051” pid or process id:

Step 2: Add SSH Key to SSH Agent

In order to add the SSH key to the SSH agent, execute the below-provided command:

$ ssh-add ~/.ssh/id_rsa

According to the below-given output, the SSH key has been added successfully:

We have efficiently explained the process of adding an already generated SSH key to the Git bash.

Conclusion

To add the SSH key to the Git bash, first, generate the SSH key using the “ssh-keygen” command. Then, launch the SSH agent. After that, execute the “ssh-add ~/.ssh/id_rsa” command to add the SSH key. This article demonstrated the method to create a new SSH key and add it to the Git bash.

Share Button

Source: linuxhint.com

Leave a Reply