How to Clone Github Private Repository with SSH
Cloning a repository from GitHub can be tricky sometimes. There are some ways to clone a private repository on your machine. The most popular ones are using personal access tokens and SSH keys.
Using a GitHub personal access token is relatively easy, you just need to generate a PAT, and then append the token on the clone URL, but this will expose risk to your repo. If your token is leaked, then your repos are at risk.
Another way that is less risky to do is by using the SSH key, Here is how to do it on Ubuntu.
For this tutorial, I will be using Ubuntu 22.04LTS. But for now, I will show you on the Ubuntu desktop, but the method will be the same.
Step 1: Make Sure Git is Installed
You want to clone something from GitHub, of course, you need Git installed on your machine. We are using Ubuntu, Git should be installed by default. But its not hurt to check ;)
First check it by typing:
git --version
If Git is installed, you'll see this.
Otherwise, you need to install Git, You can check on how to install Git on Ubuntu.
Step 2: Generating SSH Key
This is the most important step, generating an SSH key from your machine.
To create a new SSH key you can create it using the ED25519 algorithm, associating with your email. You should replace the email below with your email address.
ssh-keygen -t ed25519 -C "your-email@example.com"
Next, the machine will ask about the SSH key file save directory, we will keep it by default. Press enter.
Then you will be asked to enter the passphrase, we need to keep the key passphrase empty for easier access. Press Enter twice.
If the creation is successful, this will be shown, this is the location of your public and private key.
Then you can copy your SSH key using cat
based on your save directory. This is the case for me.
cat /home/ubuntu/.ssh/id_ed25519
Copy all the content, and we will enter this into GitHub Settings.
Step 3: Add SSH Key to GitHub
You have created your SSH key, after copying the key, we will proceed to give access to this machine on our private repository.
Open GitHub, and direct to Settings on your profile
Then, direct to the SSH and GPG Keys
menu. Click on New SSH Key
Paste your SSH key and then give a title. Then click Add SSH key
If successful, you will see your key has been added.
Step 4: Cloning the Repository
After adding the key, you can directly clone the repo using git clone command, but this time, using SSH option. Copy this from the repo.
Then, Clone it on the machine.
git clone git@github.com:username/repo-name.git
Type yes
if needed
Voila! your repo is now cloned to your systems. you can check it by typing
ls