When working with GitHub, you will need to identify yourself. The default way is using your username and password. However, you can also create an SSH key-pair that can be used instead.
By using this technique, you can generate a password and not have to use it each and every time.
You can also reuse your machine’s generated key-pair for other SSH services.
A private and public key is created locally on your machine, and the private key always stays on your own machine. The public key can then be shared with services such as GitHub.
When a connection is made between your machine and GitHub, the keys will be checked against one another and your will be granted access to continue.
It is also not possible to reverse-engineer the cryptography used behind SSH keys. So it’s a nice, simple and safe way to progress.
Generating an SSH key-pair
You will want to start by generating an SSH key-pair
You may already have some keys on your machine, you can tell this by opening the terminal and looking in your user’s
.ssh directory. as follows:
id_rsa.pub file is shown, then you can continue as is, without having to create a new one.
If you didn’t see anything, then you will need to create a new key-pair as shown below.
Use the below syntax to create a key-pair using the
ssh-keygen command-line tool. Make sure to replace your own email address:
(Newer versions of
ssh-keygen use the
-o argument; if you get an error, just remove the
-o and try again)
Just follow the prompts, but when asked where to save it, use the default location:
When presented with
Enter passphrase, you can optionally enter a password of your choice, but we will progress with a blank one, so just press
Enter. Then confirm with the same input.
Now your key-pair will be created and is available in your
Adding your public key to GitHub
Now that we have a key-pair, we need to get it ready to input into GitHub. Use the
cat command-line tool to print out the contents of our public key to the terminal.
The output should look something like this:
Copy this to your clipboard.
New SSH key and enter a name for this key (computer name works), as well as the public key you just copied to your clipboard, into the
key section, as below:
When ready, click
Add SSH key to submit the new entry.
If you are prompted for your password, enter it!
Testing and Using your SSH key
You can now use the
Use SSH option when cloning a repository:
This way it will automatically use the private key stored on your computer instead of prompting for a password each time.