Using SSH key with Github

Details

Using SSH key with Github

Beginning August 13, 2021, Github.com no longer accepts account passwords when authenticating Git operations.

One of the allowed ways for authenticating Git operations is through using SSH key.

This is a two step process. 1. Generate SSH key 2. Uploading public SSH key to GitHub

1. Generating SSH key

1. Open a terminal (cmd on windows)

2. On your home directory type the following command ssh-keygen -t rsa

3. This will prompt you to enter a filename to store the key. 

  • Just press enter to accept the default filename (/Users/you/.ssh/id_rsa)
  • Then it will ask you to create a passphrase. This is optional, either create a passphrase or press enter for no passphrase
  • When you press enter, two files will be created
    • ~/.ssh/id_rsa
    • ~/.ssh/id_rsa.pub
  • Your public key is stored in the file ending with .pub, i.e. ~/.ssh/id_rsa.pub


2. Uploading public SSH key to GitHub

  1. Copy content of your public key file id_rsa.pub to your clipboard.
  2. Visit https://github.com/settings/profile
  3. On the left-hand side menu, you will see a link "SSH and GPG keys"
  4. Click on that link which will take you to a page where you can enter your public SSH key that you copied earlier.
  5. Click the button which says ‘New SSH key’
  6. Then enter a title e.g. newMBP
  7. Paste the public SSH key in the key textbox
  8. Click "Add SSH key"

Switching remote URLs from HTTPS to SSH

In case your repository is set to use HTTPS protocol then you will have to switch to SSH to start using SSH key based authentication.

  1. Open Terminal.
  2. Change the current working directory to your local project.
  3. List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
  4. Change your remote's URL from HTTPS to SSH with the git remote set-url command.
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
  5. Verify that the remote URL has changed.
    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)

With this you should be all set to start using SSH key based github authentication.