SSH Key for GitHub

The SSH protocol, also referred to as Secure Shell, is a method for secure remote login from one computer to another. An SSH key is an access credential in the SSH protocol. Basically, instead of using username and password to identify yourself, you use a key, the SSH key. An SSH key is in fact a key pair: a private key that is stored on your computer, and a public key that is the one you share with services like GitHub.

Create your SSH key

Open your Terminal. First, we need to go to the .ssh directory where the keys are stored: cd ~/.ssh. To generate the key, type as following, using the email you use to login to your GitHub account: ssh-keygen -t ed25519 -C "email@domain.com". You will then be prompted to choose a file name and location to save the key, suggesting you a default location, something like: /Users/username/.ssh/id_ed25519. Then you will be prompted to enter a passphrase. Although this step is optional, it is recommended to add an extra layer of security to your key. Enter your paraphrase (empty for no passphrase). Once the key generation process is complete, you will receive a message saying where your identification (id_ed25519) and public key (id_ed25519.pub) have been saved. As said, in the directory chosen, you should see these two new files. The public key is the one you'll share with GitHub. Remember to keep your private key safe and secure, and do not share it with anyone.

Share your SSH key with GitHub

Open your Terminal. To automatically load keys into the ssh-agent and store passphrases in your keychain, open the config file: open config. And add these following lines:

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Then add your SSH private key to the ssh-agent and store your passphrase in the keychain: ssh-add --apple-use-keychain ~/.ssh/id_ed25519. Enter your paraphrase. Now your identity is added. Copy your public key: pbcopy < ~/.ssh/id_ed25519.pub. Log in your GitHub account, click on your Profile picture, click on Settings, click on SSH and GPG keys. Click on New SSH key button. In the Title field, you can type the name of the computer you use, for example: office laptop, so you know which computer the key is associated to. Then in the Key field, paste the key (previously copied from the Terminal).

Use your SSH key

Now the key is registered, let's clone a repository with your SSH Key, to see if it works.
Open the Terminal, go to your Desktop for example: cd ~/Desktop. Add your SSH private key to identify yourself: ssh-add --apple-use-keychain ~/.ssh/id_ed25519. Clone a repository with your SSH key: git clone git@github.com:username/repositoryname.git.

Resources

Updated on February 2, 2024.