When dealing with a GitHub repository using commands, you will frequently need to identify yourself with your username and password. Instead, you can use an SSH key to authenticate yourself. As a result, entering the email and password will no longer be necessary.
SSH keys come in pairs, with a public key shared with services such as GitHub and a private key held solely on your machine. You will be allowed access if the keys match.
An SSH key is an access credential in the SSH protocol. Its function is similar to that of user names and passwords, but the keys are primarily used for automated processes.
Effort : 30 mins
After completing this reading, you will be able to:
- Generate an SSH key
- Adding an SSH key to GitHub
If you intend to run this lab on your system, please ensure you have the following:
- A basic understanding of working with the bash terminal
- A Laptop or PC with Windows installed
- A Internet connection
- Git installed on your local machine. For more details on accomplishing this, review Installing & Setting Up Git on Windows.
- A GitHub account.
- The latest version of Visual Studio Code is installed on your machine.
To create an account in GitHub, complete the following steps:
1 - Go to the Join GitHub page and signup and create an account.
Note: If you already have a GitHub account, log in now.
2 - For more details on accomplishing this, review: How to get started with git and github
To generate an SSH key, complete the following steps:
1 - Launch a terminal. If you are using Windows, launch Git Bash.
2 - Type the following command in your terminal, replacing your email address
with the email address that is linked to your GitHub account. When you have typed the command, press Enter.
ssh-keygen -t rsa -b 4096 -C "your email address"
A new SSH key is generated.
3 - You will be prompted to enter a directory to save the key. You can simply press Enter to accept the default location, which is a .ssh folder in the home directory. This means you will be able to locate the key in ~/.ssh/id_rsa
.
4 - You will be prompted to choose a passphrase. You also have the option not to create a passphrase. To skip the passphrase, press Enter twice to confirm that the passphrase is empty.
5 - Optional: To navigate to the .ssh directory, and check the contents of the directory, run the following commands in the terminal:
cd ~/.ssh
and then,
ls
When you list the contents of the .ssh directory, you should see
id_rsa
andid_rsa.pub
in the list of contents, whereid_rsa
is the private version of your key andid_rsa.pub
is the public version of your key.
6 - You now need to add the SSH key to the ssh-agent, which helps with the authentication process. To start the ssh-agent, run the following command in the terminal:
eval "$(ssh-agent -s)"
7 - To add the key to the agent, run the following command in the terminal:
ssh-add ~/.ssh/id_rsa
To add an SSH key to GitHub, you need to copy the SSH key that you generated in the previous lab. Open a terminal and then complete the following steps:
1 - In the terminal, run the following command:
cat ~/.ssh/id_rsa.pub | clip
Note: If
clip
doesn't work, runcat ~/.ssh/id_rsa.pub
in the command line and copy the output.
2 - Sign in to GitHub. At the top right, click the drop-down menu on your profile image and select Settings.
3 - From the "Personal settings" menu, select SSH and GPG keys, as shown in the following image:
4 - Click New SSH key.
5 - Enter a title for the new SSH key.
5.2 - In the Key field, paste the key that you copied in step 2. Genarating an SSH key, above.
Note: The pasted key should include Your email address at the end.
5.3 - Hit the button Add SSH key
6 - Click Add SSH Key. The SSH key is added to your account.
Congratulations! You have now learned how to generate the SSH key and add an SSH key to GitHub.
There are a lot of helpful Git and GitHub learning resources on the web. This is a short list of my favorites!
-
GitHub Skills Learn how to use GitHub with interactive courses designed for beginners.
-
GitHub Live Training, with a hands-on, project-based approach for those who love the command line and those who don't.
-
Code School: Git Real. Learn basic concepts of Git version control. Explore staging, cloning, branching, and collaborating with Git.
Now you know more about generating and adding an SSH key to Github, Feel free to these steps to make sure you understand how they work.
Download this Git commands cheat sheet to have all Git commands in one place for future use.
If you want to follow along with more advanced stuff in the next article I'll talk about GitBash Installation on your Desktop.
Thanks for the read! Now go practice & build something awesome!