How to use SSH keys with GitHub repositories
Tags: ssh, github, beginners, tutorial
Published: 2022-12-21 16:48:18

SSH (Secure Shell) is a network protocol that allows you to securely connect to a remote computer. It is commonly used for remote command-line access and remote command execution.

In this tutorial, we'll go over how to use SSH to work with GitHub repositories on a MacOS or Linux system.

## Generating an SSH key

Before you can use SSH to connect to a remote system, you'll need to generate an SSH key. An SSH key consists of a public key and a private key. The public key is used to encrypt data, while the private key is used to decrypt it.

To generate an SSH key on a MacOS or Linux system, open a terminal and enter the following command:

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

Replace "your_email@example.com" with your email address.

This will generate a new SSH key pair and prompt you to enter a file in which to save the key. You can accept the default location by pressing Enter.

You will then be prompted to enter a passphrase for the key. This is an optional security measure that allows you to protect your private key with a password.

## Adding the SSH key to your GitHub account

Once you have generated an SSH key, you'll need to add it to your GitHub account. To do this, follow these steps:

1. Open a terminal and enter the following command to print your public key:

`cat ~/.ssh/id_rsa.pub
`
2. Copy the output of the command to your clipboard.

3. Go to your GitHub account settings and select the "SSH and GPG keys" tab.

4. Click the "New SSH key" button.
 
5. In the "Title" field, enter a descriptive name for the key (e.g. "Work laptop").

6. In the "Key" field, paste the public key that you copied to your clipboard.
 
7. Click the "Add SSH key" button to add the key to your account.

## Cloning a repository using SSH

Now that you've added your SSH key to your GitHub account, you can use SSH to clone a repository. To do this, follow these steps:

1. Go to the repository page on GitHub.
2. In the "Clone or download" dropdown, click the "Use SSH" button.
3. Copy the SSH URL of the repository to your clipboard.
4. Open a terminal and navigate to the directory where you want to clone the repository.
5. Enter the following command to clone the repository:
`git clone <repository_url>
`
Replace `<repository_url>` with the SSH URL of the repository that you copied to your clipboard.

The repository will be cloned to the current directory.

## Working with a repository using SSH
Once you have cloned a repository using SSH, you can work with it as you would with any other local repository. To push changes to the repository, use the git push command.

For example, to commit and push changes to a repository, you can use the following commands:

```bash
git add .
git commit -m "Commit message"
git push
```
Note that you may be prompted to enter your SSH passphrase when pushing changes to the repository.

## Troubleshooting
If you encounter any issues while using SSH, there are a few things you can try:

- Make sure that your SSH key is correctly added to your GitHub account.
- Make sure that you are using the correct SSH URL for the repository. You can find the SSH URL in the "Clone or download" dropdown on the repository page on GitHub.
- If you are prompted for a password when connecting to the remote system, make sure that you have entered the correct SSH passphrase for your private key.