SSH for GitHub

Overview

  • This method will allow you to interact with your repos hosted on GitHub without having to enter your login credentials each time.
  • You will create an SSH key on your local machine. By “local machine,” we mean the machine where you will be working and pulling to, e.g your laptop or Rivanna. In other words, it’s local relative to GitHub.
  • SSH keys have a public and private component. These are hash strings that are stored in files. Both will be generated on your machine.
  • You will copy and paste the generate public key to your GitHub account.
  • Going forward, you will clone from your account using the SSH protocol.

Steps

Part A

On your local machine

Get to the command line (i.e. the shell).

  1. On a Mac, open Terminal.
  2. If you are on Windows and you have admin rights, first install git-bash. Otherwise follow this tutorial from Microsoft.
  3. On Rivanna, either connect via SSH or use Rivanna Shell Access (under Clusters).

Move into your root directory and enter cd.

Generate the key.

  1. Enter: ssh-keygen -t ed25519 -C "your_email_id@example.com", using your email address.
  2. Be sure to use the email address associated with your GitHub account in the above command.

At the prompt, type in a secure passphrase.

  • You don’t have to do this, but it is advised.
  • Create a memorable sentence.
  • A good passphrase should have at least 15, preferably 20 characters and be difficult to guess. It should contain upper case letters, lower case letters, digits, and preferably at least one punctuation character.

Add the key to ssh-agent.

  1. Enter: eval "$(ssh-agent -s)"
  2. Enter: ssh-add ~/.ssh/id_ed25519

If you’re using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain. Follow the instructions here (at step 2).

Part B

On your GitHub account

Get the public key that was just generated.

  1. Enter: more ~/.ssh/id_ed25519.pub
  2. Copy the result to your clipboard (e.g. by blocking off the line and entering Ctrl-C).
  3. The key should begin ssh-ed25519 and end with the email address you used in generating the key. In between it will have a long string of alphanumeric characters.

On GitHub, go to your account settings and select “SSH and GPG Keys” from the side menu.

  • A link to your account settings can be found in the drop-down list produced by clicking on your user icon in the upper right of the website.

Under “SSH keys,” press the “New SSH Key” button.

  1. Add a brief title describing the context of the key, i.e. the local machine where it was generated, e.g. Rivanna.
  2. Choose “Authentication Key” as the Key type.
  3. Paste the key into the Key text area.
  4. Submit the form by pressing “Add SSH key.”

You are now good to go. Whenever you clone a site from your GitHub account, choose the SSH link.

Information Sources

The GitHub site has lots of excellent documentation. Here are some pages you may find useful.