Welcome back to another series of Git related article! Just kidding. But, perhaps I should really consider it. Anyway, here are my first two articles about Git: the commands that I often use and how to set up your repo.
In this one, I am writing about Git SSH
which is useful for cloning repositories. I decided to write this because I often forgot about it. And when I have to visit it again due to multiple reasons (typically changing of laptops), I need to redo it again except, my muscle memory was so used to cloning by using the URL and GitHub decided to “block” it by saying we are better off using SSH feature!
I can’t find the image about GitHub preventing me from using the URL to clone, on the internet. If I encounter it again, I will screenshot it and update it here
The issue gets more complicated when I decided to search for the How and GitHub documentation comes out but the instruction is not very clear! At least to me…
Yes, me being me, I just have to write this guide, for myself and for you that needs it. Regardless, using Git SSH
to clone is actually very convenient.
I know you can set your git config
or if you are using the UI pop up to log in, they work well too. However, SSH is good if you prefer perfect control. Other than that, it is just cool. Just kidding. I mean, personally for me it is because of the one-time setup process, and I am fully aware what is used and where stuff is stored unlike the rest of the methods which you either don’t know where your credentials are stored in or it is within Window’s / Macs’ credentials vault.
You will get what I mean after knowing how it works. Here it is.
The Process
SSH (Secure Shell) provides a secure channel for data communication between two computers. In the context of Git, it is used to authenticate you with your remote repository.
How this works is through your private and public keys.
Steps to set up SSH (works in Windows, Linux, and Macs):
- Generate an SSH Key pair
- Open your Git Bash (or terminal in Linux or Macs)
- Generate a new SSH key pair. You will be prompted about what file name you want it to be, just like the screenshot below. Use this command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com”
- After that you will be prompted for password. You can just skip it by pressing “Enter” if you don’t want any password.
- After that, your key pair will be generated and located inside
.ssh
directory. - The folder
.ssh
is always located under your~
. - If you have more than one GitHub account with different email addresses, you can always generate more SSH keys by providing different names (e.g.
id_rsa_0
,id_rsa_1
, etc.), and then add the keys to respective GitHub accounts.
- Add the Public Key to your GitHub account
- Look for your
.ssh
directory by going to~
. You can do so by typing:cd ~
- Inside your
.ssh
directory, you will have the file inside. Example is as shown by the screenshot below. - Look for the file that ends with
.pub
, and see the content. You can do so by typing:cat <file>.pub
. Otherwise, you can just open it in a text editor. - There will be a long string displayed. Copied the whole thing as this will be added into your GitHub.
- Go to your GitHub account settings, navigate to “SSH and GPG keys”, and click “New SSH key”.
- Paste the copied public key into the key text area, give it a title, and click “Add SSH key”.
- Look for your
- Test the SSH connection
- Once done, you can either try by cloning your repository directly or do testing.
- For testing, do so by running:
ssh -T git@github.com
- If successful, you should see a welcome message from GitHub. Mine looks like the screenshot below.
- For cloning your repository, do so by running:
git clone git:github.com:<username>/repository.git
- For testing, do so by running:
- Once done, you can either try by cloning your repository directly or do testing.
So why do I like this? To me the whole thing is just so transparent and it is a set-up-and-forget kind of way. This is also why every time I need to set it up, I often would have forgotten how to do it by then due to the long period of not doing it. Moreover, when you have more than one key for more than one account, all your key pairs are still located inside the .ssh
directory! This means it is easy to maintain. The cool part about this is you can clone repositories seamlessly from different account!
Wrap-Up
This article is written as a guide to set up Git SSH that is actually more convenient in a sense that you don’t need username or memorising of password. Simply let your machine sets up the key pair, and then copy and paste the key to your GitHub account for access.
With that, that’s all for this article. If you find this article useful, feel free to share with other people. If you have any feedback, feel free to comment in the comment section down below. Stay cool, stay safe.
Thank you for reading.