SSH Key in Git: How to Set Up an SSH Key for GitHub

Git is the most popular tool for version control, and GitHub is the most widely used platform for storing your code in the cloud. However, having to enter your username and password every time you interact with GitHub can be quite inconvenient.

This is where the SSH key comes in. The key is used to establish a secure connection between your machine and GitHub, eliminating the need to repeatedly enter your credentials. In this article, I will guide you step by step on setting up an SSH key in GitHub to optimize your workflow.

Generating the SSH Key

The first step is to generate the SSH key. You can do this by running the command below to generate the SSH key. Don't forget to replace codigoaoponto@email.com with your email registered on GitHub:

ssh-keygen -t ed25519 -C "codigoaoponto@email.com"

During this process, you will be asked about:

  • Adding a password to the SSH key.
  • Changing the directory where the SSH key will be saved.

In this tutorial, we will not worry about these options. For both questions, you can simply press Enter. After these two questions, the SSH key will be generated.

If, for some reason, you want to generate the key in rsa format, the command will be slightly different, but the process will be identical, also requiring just pressing Enter for the questions:

ssh-keygen -t rsa -b 4096 -C "codigoaoponto@email.com"

Obtaining the Generated SSH Key

The second step is to copy the generated SSH key, and you can do this by running the command below, where key_directory should be replaced with the path to the public SSH key file:

cat key_directory

If you followed all the previous tutorial steps, the SSH key was generated in the default directory: ~/.ssh/id_ed25519.pub (or ~/.ssh/id_rsa.pub if you generated the key in RSA format). If you chose to save the SSH key in another directory, simply place the path to that directory after the cat command.

To display in the terminal the public SSH key saved in the default directory, use:

cat ~/.ssh/.id_rsa.pub

The cat command prints the contents of a file in the terminal. In this case, it will display the public SSH key, which you must copy.

Adding the SSH Key to Your GitHub Account

Now that your SSH key is copied, the next step is to link it to your GitHub account:

  1. While logged into GitHub, in the upper right corner, click on your profile picture and select Settings.
  2. In the left menu, click SSH and GPG keys.
  3. Click the New SSH key button.
  4. In the Title field, enter a description for the key, such as "Personal Computer" or "Work Computer".
  5. In the Key field, paste the contents of your public key that you copied earlier.
  6. Click Add SSH key to save.

Once this is done, when you run a Git command such as git pull or git push, GitHub will use the SSH key you generated and configured.

::img{src="images/blog/git/add-new-ssh-key-on-github.webp" alt="GitHub page for adding SSH key" width="658" height="488"}::

Conclusion

This article guided you through generating the SSH key and configuring it in GitHub. If you want more complementary content on the subject, you can watch this video on the Código ao Ponto YouTube channel or access the official GitHub documentation.