Connecting to GitHub

We can connect to git repositories in two ways: HTTPS and SSH.

  • HTTPS: asks for an access token every time we do a push to a repository.

  • SSH: enables us to use Git commands without requiring username and token.

SSH is the preferred method to communicate with GitHub in the industry and fairly simple to setup:

  1. We first need to generate an SSH key on our local machines.

  2. After generating the key, we add our public key to our account on GitHub.com

Once we complete the step 2, we will then be authenticated for Git operations over SSH.

Generating an SSH key on Mac

  1. Open Terminal.

  2. Paste the text below, replacing the email address used on your GitHub account .

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This creates a new SSH key, using the provided email as a label. When you're prompted to "Enter a file in which to save the key", you can press Enter to accept the default file location.

> Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM): [Press enter]
  1. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."

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

Adding your SSH key to the ssh-agent

If we don't want to type our passphrase each time we use the key, we will need to add it to the ssh-agent.

  1. Start the ssh-agent in the background.

    $ eval 'ssh-agent'
    > Agent pid 59566

Depending on your environment, you may need to use a different command. For example,

  • you may need to use root access by running sudo -s -H before starting the ssh-agent,

  • or you may need to use exec ssh-agent bash or exec ssh-agent zsh to run the ssh-agent.

  1. 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.

  • First, check to see if your ~/.ssh/config file exists in the default location.

    $ open ~/.ssh/config
    > The file /Users/YOU/.ssh/config does not exist.
  • If the file doesn't exist, create the file.

    touch ~/.ssh/config
  • Open your ~/.ssh/config file using vim or the text editor of your preference, then modify the file to contain the following lines. If your SSH key file has a different path than the example code, modify the file path to match your current setup.

    Host github.com
      AddKeysToAgent yes
      UseKeychain yes
      IdentityFile ~/.ssh/<private_key_file>

Notes:

  • If you chose not to add a passphrase to your key, you should omit the UseKeychain line.

  • If you see a Bad configuration option: usekeychain error, add an additional line to the configuration's' Host *.github.com section.

    Host github.com
      IgnoreUnknown UseKeychain
  1. Add your SSH private key to the ssh-agent and store your passphrase in the keychain with the name of your private key file.

    ssh-add -K ~/.ssh/<private_key_file> 

In macOS versions prior to Monterey (12.0),

  • -K flag represents --apple-use-keychain

  • -A flag represents --apple-load-keychain

If you continue to be prompted for your passphrase, you may need to add the command to your ~/.zshrc file (or your ~/.bashrc or file for bash).

  1. Copy the SSH public key to your clipboard.

$ pbcopy < ~/.ssh/<public_key_file>.pub
# Copies the contents of the <public_key_file>.pub file to your clipboard

Add the SSH public key to your GitHub account

  1. Go to Profile>Settings>SSH and GPG keys.

  2. Click New SSH key or Add SSH key on the upper right corner.

  3. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".

  1. Select the type of key, either authentication or signing. For more information about commit signing, see "About commit signature verification."

  2. In the "Key" field, paste your public key.

  3. Click Add SSH key.

  4. If prompted, confirm access to your account on GitHub. For more information, see "Sudo mode."

Last updated