To add a SSH key to GitHub it is necessary to work both in your computer and in GitHub web page. Lets start with the computer from which you want to commit code to your GitHub repositories. Later we will deal with GitHub.
@ your computer
First start by generating a ssh key. To generate a ssh key got to the command line and type:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/cadoado/.ssh/id_rsa): /Users/cadoado/.ssh/github_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/cadoado/.ssh/github_rsa.
Your public key has been saved in /Users/cadoado/.ssh/github_rsa.pub.
The key fingerprint is:
SHA256:5egVnuUSe4NKS4l8HU4mnioQk4sacmjI3upq9eQ9Bfk cadoado@Cados-MBP
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| + o B . |
|oo + . = & X |
|*o+ o S @ + |
|=o.o . * E o . |
|....= o = |
| .. + o |
|=o . |
+----[SHA256]-----+
The passphrase can be an empty string (just press Enter). Now you can check the key pair was generated:
The file github_rsa is the private part, that you shell never ever disclose. The file github_rsa.pub is the public part, that you distribute whenever requested to.
Assign the key to GitHub editing – or creating – a config file:
$ vi ~/.ssh/config
Add these lines to the config file (here you must refer to the private part of the key):
Finally copy/paste the public part of the previously generated key. Give the key a name because in the future you may end up with several keys (for each device you want to commit code from)
Final remarks
Now you can communicate conveniently and securely with GitHub, avoiding constant typing of your user/password.
You may need to reset the project origin in your local machine, please refer to the post Changing GitHub project origin.