Connecting to Git Using a Specified SSH Key

Contents

Background

Today, on my own computer, I was helping a friend set up his private Github repository. During the process, I used SSH for git clone and git push operations. Despite having added a new SSH key to the computer and linking it with Github, I still encountered authorization problems.

  1. The way to generate a new SSH key is to type ssh-keygen in the terminal, then, at the first prompt, fill in the absolute path of the filename you want (like /home/rex/.ssh/id_rsa_NEW in the picture), instead of directly skipping it by pressing Enter. Otherwise, it will overwrite the original /home/rex/.ssh/id_rsa on the computer.

    Remember to enter a name different from id_rsa

  2. You can find the newly generated SSH key in ~/.ssh/. Then go to Github and link this new SSH key.

Solution

I found that adding GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_NEW" before the git command that has the permission problem allows for smooth execution. For instance:

1
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_NEW" git pull origin main
0%