How to add SSH keys to your GitHub account

The guide is for windows 10 centric. The steps are 99% same for Linux/MacOS users as all the 3 OS can use gitbash, where the scripts are to be fired, but the only difference might be the commands that are being used. The underlying principles are same.

Why SSH?

Using SSH, you don’t have to provide your username and password for each visit on github. These “visits” include git operations like: push, pull, fetch, clone, etc. You can use HTTPS method as well, but SSH is the way “real devs” handle git, and if you are here, reading this – you definitely are a “real dev”.

Step 1: Generate a SSH key

For Windows 10 users, open either powershell or gitbash on your system.

Start your powershell with “Administrative” rights

powershell

Or you can start gitbash, my personal favorite! Right click in the folder where you want to clone the repo (preferably) and select Git Bash Here

gitbash
gitbash

Next, paste the below script in your gitbash

ssh-keygen -t ed25519 -C "your_github_email@example.com"

Now, understanding the above script:
The ssh-keygen generates a combo of personal and private encrypted SSH tokens. There are primarily two kinds of encryption algorithms used during SSH creation:

  1. ed25519
  2. rsa

For a noob like you (and me), we just need to know that the ed25519 algorithm is better for now. If you are also cursed with the curiosity bug like me, then read about the differences here.

Next, select the location and the file name you want for your ssh key. Once you hit Enter on the key-generation script, you’ll see something like this, where /c/Users/you/.ssh/ is the default location and id_ed25519 is the default key name: both for public key (with .pub extension) and for the private key.

Enter a file in which to save the key (/c/Users/you/.ssh/id_ed25519)

Ideally, do not change the location of the ssh-key. If you are using more than 1 ssh-keys/github account on the same machine, you can change the file name instead. For example, if I want to change the key name here, I’d have to give the complete path here:

Enter a file in which to save the key (/c/Users/you/.ssh/id_ed25519): /c/Users/you/.ssh/id_ed25519_my_custom_key

Hit Enter

You’ll be asked to set a password for the key. This password will be used every-time you use the key. You can leave it blank if you are using this on a personal computer.

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Once you pass that step, ssh-key will be generated with a combo of:
1. Public key – with .pub extention
2. Private key – without any extention

gitbash to with ssh-key
gitbash to with ssh-key

A detailed guide is present here

Step 2: Adding SSH key to SSH-AGENT

In simple terms, ssh-agent manages your ssh-keys. Keys are stored in an unencrypted manner and are used for protecting the keys and during ssh communication. Read more about ssh-agent here.

In windows, before you can add the ssh-key, you need to ensure that it is running. Run this in your git-bash

eval `ssh-agent`

If you see something like Agent pid 59566 then your ssh-agent is running in the background. Your agent pid can be different. No issues there!

Add your ssh-key

ssh-add ~/.ssh/id_ed25519_my_custom_key

That’s it. They key is added!

Step 3: Adding SSH key to your GitHub account

The ssh-key with the .pub extension is to be added to your GitHub Account. Copy your public ssh-key.

cat ~/.ssh/id_ed25519_my_custom_key.pub | clip

And paste it in your github profile’s settings page.

Settings on Github

And paste the key in the “purple” area. Give a title to recognize the key!

Adding ssh-key to github

Step 4: Enjoy!

That’s it. Now you can perform operations like clone, push, pull, fetch and many more without providing username and password repeatedly.


Thank you for reading. I hope you have a wonderful week ahead!
Feel free to reach out with suggestions and feedback. You can either email me or on the bird app (twitter) here.

For weekly updates on tech and such stuff, feel free to subscribe to my blog.

Leave a Reply

Your email address will not be published. Required fields are marked *