How to set up an ssh key for a git repo

Nick Hass
1 min readJun 17, 2024

--

Make sure to follow all these steps in order!

mkdir -p ~/.ssh/your_github_account
touch ~/.ssh/your_github_account/id_rsa

# start the ssh-agent
eval `ssh-agent -s`

# Generate your ssh keys
ssh-keygen
# now specify this location to save your id_rsa: ~/.ssh/your_github_account/id_rsa or alternatively just hit Enter to save to the current directory.
# enter
# enter

chmod 600 ~/.ssh/your_github_account/id_rsa

ssh-add ~/.ssh/your_github_account/id_rsa

Now, go to your github or gitlab account and add an ssh key. You will have to paste the contents from your ~/.ssh/your_github_account/id_rsa.pub file into the textbox in github/gitlab. Give your key a name. I would name it the name of your computer and where that ssh key is located.

Explanation time… for a background on what is happening, you are putting your public key (id_rsa.pub) in the github/gitlab website, and then with ssh-add, you are now giving it your private key (id_rsa). This handshake where the public key validates you as a user with your private key is what gives you ssh access to your github/gitlab account.

Now you can check that you have made the connection by running ssh -T git@your_github_account.com (you can get this url from gitlab where it gives you the Clone with SSH url). Add it to the list of known hosts when it prompts you

You can know clone your repo git clone <clone_with_ssh_link_to_your_project>

--

--