How to connect your git to github using ssh keys
- Generate the id_rsa (default) key on windows 10
- Add the public key to the github website
- Start the agent on a git bash session
- Pass the key to the agent
- Test the connection in github
- Auto-log in visual studio code with your key (.ssh\config file)
- Generate a second ssh-key (helloworld_rsa)
- Recap, link helloworld_rsa key to Github
- Visual studio still ask me to log in (switch from https to ssh)
- I prefer user HTTPS over the ssh key auth, how it works?
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 “firstname.lastname@example.org”
You can leave blank the other prompts like name and passphrase
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
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 email@example.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
firstname.lastname@example.org: 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
You can add a file called config in your .ssh folder with the following configuration
Host *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
Now we should have the helloworld_rsa and the helloworld_rsa.pubb in our .ssh folder
Recap, link helloworld_rsa key to Github
- Copy the content of helloworld_rsa.pub and add the key on the key list on the github site
- Open git bash and type
`ssh-agent -s`to open the agent
- Type ssh-add -L to check if there are already keys, if yes remove with ssh-add -D
- Add the new key to the agent using the command ssh-add ~/.ssh/helloworld_rsa
- Create the file config in the .ssh folder with the content described above (the last line should be IdentityFile ~/.ssh/id_helloworld)
- Verify the github connection typing ssh -T email@example.com
- 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)
- 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 firstname.lastname@example.org:username/repo.git
This will set your repo to work with the SSH key.
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)
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
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.