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

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 "your_email@domain.com"

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 id_rsa_github.pub

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.

SWITCH AN EXISTING HTTPS REPO TO SSH

Navigate to the project's directory and run the following

$ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

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

Test SSH connection to Github

$ ssh -T git@github.com

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

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.