How to use Github SSH Key and Switch HTTPs based projects to SSH (My Rough Note)

Play this article

Generate new SSH

Go to the ssh directory

$ cd ~/.ssh

View the existing ssh keys

$ ls

Generate new key

$ ssh-keygen -t rsa -b 4096 -C ""

After generating a new key, you'll be given the private and the public key. The public key ends with .pub. It's the public key that we'd add to our github repo.

Add the ssh key to your registered keys. We're to add the private key in this instance.

$ ssh-add ~/.ssh/id_rsa_github

// id_rsa_github being the name of the key we just generated or the name you entered.

Copy the public key and paste it on github

$ cat

This will read the key's content to the terminal. Highlight it and copy it.

Then, head over to github. Settings > SSH and GPG Keys. Then click on add new key button. Once you've added that. You should be able to perform your normal activities as before.


Navigate to the project's directory and run the following

$ git remote set-url origin

This will switch the remote url from https to ssh based.

Test SSH connection to Github

$ ssh -T

Another important thing to note, is that you don't need to be including the keyword sudo anymore while running any of the commands as this would result to error.

Sample Error Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.