github SSH Keys setup on Windows 10

How to connect your git to github using ssh keys


Always check if you have already the id_rsa keys
id_rsa and id_rsa.pub are made by default and you should check in the .ssh folder before create new one using ssh-keygen
On windows 10 the ./ssh folder is located on C:\Users\currentuser\.ssh
If there is no folder, the ssh-keygen will create it

Generate a default key: id_rsa

First you need a pair of key, you can create those from the command prompt

ssh-keygen -t rsa -b 4096 -C “mygithub@personalmail.com”
You can leave blank the other prompts like name and passphrase

ssh-keygen -t rsa -b 4096 -C
Generate a default key: id_rsa

This command will create 2 keys one private one public in your user folder under .ssh

Note: Never show your private keys

Add the publick key on github website
Copy the content of the id_rsa.pub
Once you have the public key you have to paste it “add new ssh key” github on https://github.com/settings/keys

SSH public keys on github website

Start the agent on a git bash session
Open a git bash editor and type start the agent

eval `ssh-agent -s`

Pass the key to the agent
Once the agent is open you have you can add it typing ssh-add ~/.ssh/id_rsa
You can check the keys inside the agent typing: ssh-add -L

Test the connection in github
ssh -T git@github.com
Type yes (note the fingerprint is the public one of github is not the same of your public key!)
> Hi your_user_name! You’ve successfully authenticated, but GitHub does not provide shell access.

Now you you should be connected with your account, try to pull your project.

If you used an external bash (not in visual studio) to add the key to the agent, you may get connection error once you try to git

$ git pull
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

In this case you can just simply close visual studio and launch it from the bash prompt using the command: code
More details here: https://stackoverflow.com/a/37882140/7454921


For auto-log in visual studio code with your key
You can make a shortcode to open visual studio trough the agent : cmd /c start-ssh-agent & code
or
You can add a file called config in your .ssh folder with the following configuration

Host *
IgnoreUnknown UseKeychain
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa

For IdentityFile ~/.ssh/id_rsa type the key you intend to use (the one you added to the agent)

Generate a second ssh-key (helloworld_rsa)

You may already have id_rsa or you need make another key, in this case you have to name it during the generation process,
Lets suppose we call the new key: helloworld_rsa

Generate a different key (different location)
when asked we have to copy the full path and change the name in helloworld_rsa

Now we should have the helloworld_rsa and the helloworld_rsa.pubb in our .ssh folder

  1. Copy the content of helloworld_rsa.pub and add the key on the key list on the github site
  2. Open git bash and type eval `ssh-agent -s` to open the agent
  3. Type ssh-add -L to check if there are already keys, if yes remove with ssh-add -D
  4. Add the new key to the agent using the command ssh-add ~/.ssh/helloworld_rsa
  5. Create the file config in the .ssh folder with the content described above (the last line should be IdentityFile ~/.ssh/id_helloworld)
  6. Verify the github connection typing ssh -T git@github.com
  7. Open visual studio code, in the bash terminal check if the project is using SSH, if still https change it with the command remote set-url (see below)
  8. Pull (it should work)

I added the ssh key but Github keep asking me credentials

If github keep asking user and password is probably because you have cloned your project as https instead ssh
You can fix this going in the bash of the repo and type

git remote set-url origin git@github.com:username/repo.git

This will set your repo to work with the SSH key.

Errors

Could not open a connection to your authentication agent.

In git bash the eval connection to start the agent must be inside a grave accents ‘ (alt+96)

ssh-agent -s must be inside the grave accent

If you stated the agent in a git bash and than start another one inside de bash in visual studio terminal you may got the same error (or connection refused), in this case remember to make the config file in the .ssh h folder

I generated the keys but I do not have the .ssh folder

The .ssh folder should be in your user folder C:\Users\currentuser\.ssh
If you made the keys and you don’t find the .ssh folder you may be forget to put the full url when thy generator ask you “Enter file in which to save the key” (here).
Your key are in the main folder “C:\Users\currentuser\” delete those and make a new pair

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection

I prefer user HTTPS over the ssh key auth, how it works?

For some users HTTPS remote URL may a simpler and better way to manage Github access.

If you have the report set to use a SSH key you can use the follow command

git remote set-url origin https://github.com/username/project.git

Once you pull you will got the popup for login


If you clone your repo with https github will ask you to log in and it will store your credentials in the windows credentials



Questions? Suggestions? Please leave a comment below.

Leave a comment