Connecting to GitHub using SSH

Introduction to SSH

SSH stands for Secure Shell. It’s a protocol used to securely connect and interact with servers. When it comes to GitHub, using SSH keys can simplify the process of interacting with your repositories.

Instead of entering your username and password every time you perform a Git operation, using SSH keys involves creating a pair of keys - a private key (kept secret on your machine) and a public key (added to your GitHub account). This pair forms a secure connection between your machine and GitHub without having to supply your username and password each time.

Why should I do this? sounds complicated…

It will save you a lot of time, as you won’t have to type your username and password every single time you download a GitHub repository.

Step-by-Step Guide to Connect to GitHub via SSH

Checking for Existing SSH Keys

Before we generate an SSH key, we need to check for existing ones.

On macOS/Linux, open Terminal and enter:

ls -al ~/.ssh

On Windows, open Git Bash from the menu (it was installed together with Git) and enter:

ls -al ~/.ssh

If you see files named id_rsa.pub, id_ed25519.pub or id_ecdsa.pub then you have existing keys. If you want to keep those and not override them, you can skip the next step.

Generating a new SSH key

If you don’t have an existing SSH key or want to create a new one, here’s how you can do it:

On macOS/Linux, open Terminal and use the following command, replacing “your_email@example.com” with your GitHub email address:

ssh-keygen -t ed25519 -C "your_email@example.com"

On Windows, open Git Bash and use the same command:

ssh-keygen -t ed25519 -C "your_email@example.com"

You’ll be asked to enter a file in which to save the key, you can just press Enter to accept the default file location.

Next, you’ll be asked to enter a secure passphrase. While it’s optional, it’s good practice to enter one for an added layer of security.

The entire process would look something like this:

> ssh-keygen -t ed25519 -C "your_email@example.com"
> Generating public/private ed25519 key pair.
> Enter file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Your SSH key pair is now generated!

Adding Your SSH Key to the ssh-agent

Before adding a new SSH key to your GitHub account, we should add it to the ssh-agent software on your computer.

  1. Start the ssh-agent in the background with the following command:

On macOS/Linux, open Terminal and use:

eval "$(ssh-agent -s)"

On Windows, open Git Bash and use:

eval "$(ssh-agent -s)"
  1. Next, add your SSH private key to the ssh-agent.

If you used a custom name for your SSH key, replace id_ed25519 accordingly, else:

On macOS you can use the ssh-add command like so:

ssh-add -K ~/.ssh/id_ed25519

On Windows and Linux use:

ssh-add ~/.ssh/id_ed25519

Adding SSH key to your GitHub Account

  1. Now you need to add the SSH key to your GitHub account. First, you need to copy the SSH key to your clipboard.

    On macOS/Linux, use the pbcopy command:

    pbcopy < ~/.ssh/id_ed25519.pub

    On Windows, use the clip command:

    clip < ~/.ssh/id_ed25519.pub
  2. Go to GitHub, click on your profile picture and then click on Settings.

  3. On the left side of the page, click on SSH and GPG keys.

  4. Click on the New SSH Key button.

  5. Give your key a title (it’s useful to use the name of your machine) and paste the key into the “Key” field. It should start with ssh-ed25519 or ssh-rsa.

  6. Click the Add SSH Key button to complete the process.

Testing your SSH Connection

Finally, to ensure that everything is set up correctly:

  1. Open Terminal or Git Bash and enter the following command:
ssh -T git@github.com
  1. You’ll see a message like this:
The authenticity of host 'github.com (IP ADDRESS)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

Type yes. Then, you’ll receive a message like this:

Hi <your_GitHub_username>! You've successfully authenticated, but GitHub does not provide shell access.

If you see the above message, you’ve successfully set up SSH authentication to GitHub. Now, you can use Git without having to enter your username and password every time for each operation.

Great! Now you understand how to connect to GitHub using SSH on Windows, macOS, and Linux.

Going forward, if you want to clone any repository from GitHub, use the SSH URL which starts with “git@github.com” instead of HTTPS.

For example:

git clone git@github.com:username/repository.git

With the SSH link, your machine will use the SSH key pair to authenticate with GitHub, so you won’t have to enter your username (or email) and password each time you perform any git operations like push or pull.

Remember, if you have multiple GitHub accounts or if you use multiple machines, you will need to generate a unique ssh-key pair for each combination of GitHub account and machine. You can manage all your SSH keys within the settings of your GitHub account.

Through SSH keys, you have established a secure connection between your machine and GitHub. This will significantly simplify your work-flow, especially when working on bigger projects.

Also, make sure to keep your private key (~/.ssh/id_ed25519 in our case) confidential and secure, don’t share it with anyone. Your public key (~/.ssh/id_ed25519.pub in our case) is the one you add to your GitHub account.