Written by: Thiago Nunes Batista
Published on 04/02/2025
Last updated: 04/02/2025
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.
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:
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"
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.
Now that your SSH key is copied, the next step is to link it to your GitHub account:
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"}::
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.