Quick steps to connect to Github through SSH in Mac

By connecting to GitHub through SSH, you won’t be prompted to enter your credentials every time you pull or push to a repository; which is something you would end up doing way too often.

Step 1: Checking for existing SSH keys

It is always safe to initially check for existing SSH keys before creating a new one.

  • Open the Terminal application
  • Enter the following command to see if there are existing SSH keys
$ ls -al ~/.ssh

By default, you might end up seeing the filenames of the public SSH keys like the ones mentioned below:

Step 2: Generate a new SSH key

If you don’t have an existing public and private key pair, or don’t wish to use any that are available to connect to GitHub follow through the steps mentioned below.

  • In the Terminal application, paste the code below and substitute your GitHub email address
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

This creates a new SSH key for the email address that was provided. If everything went well you would see the log message as shown below.

> Generating public/private rsa key pair.
  • When you are prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location (as mentioned below).
> Enter a file in which to save the key 
(/Users/you/.ssh/id_rsa): [Press enter]

When you are prompted to type a secure passphrase. Enter one to secure your SSH key.

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

> Enter same passphrase again: [Type passphrase again]

Step 3 – Adding your SSH key to the ssh-agent

While adding the SSH key to the agent, use the macOS’ default ssh-add command, and not an application installed by macportshomebrew, or some other external source.

  • Start the ssh-agent in the background by running the first line shown below.
$ eval "$(ssh-agent -s)"
> Agent pid 59566
  • If you are using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/configfile to load keys automatically into the ssh-agent and store pass-phrases for your SSH key in your keychain.
Host *
    AddKeysToAgent yes
    UseKeychain yes
  • Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
$ ssh-add -K ~/.ssh/id_rsa

If you don’t have Apple’s standard version installed, you may receive an error. For more information on resolving this error, see “Error: ssh-add: illegal option — K.”

Note: The -K option is Apple’s standard version of ssh-add, which stores the passphrase in your keychain for you when you add an ssh key to the ssh-agent.

Step 4 – Adding the SSH key to your GitHub account

  • Copy the SSH key to your clipboard by running the code mentioned below. If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
$ pbcopy < ~/.ssh/
# Copies the contents of the file to your clipboard

Tip: If pbcopy isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  • Now head on to the Github website, in the upper-right corner of the home page, click on your profile photo, then click Settings.
  • In the user settings sidebar, click SSH and GPG keys (highlighted in red).
  • Click the button to the right which usually says New SSH key or Add SSH key (highlighted in red).
  • In the “Title” field (highlighted in red), add a descriptive label for the new key. For example, if you’re using a personal MacBook, you might call this key “Personal MacBook Pro”.
  • Paste your key that is copied to your clipboard into the “Key” field (highlighted in red).
  • Click the button below the key field which says “Add SSH key“. Confirm your GitHub password if you are prompted to.

Now that you have created a new SSH key in your machine and having linked the key to Github, you can clone the repositories with the SSH URL. As shown in the image below, make sure the title in the dropdown section says Clone with SSH.

Happy coding!!!


Leave a Reply

Your email address will not be published. Required fields are marked *