How to clone private github repo in the Cloud Virtual Machine or Remote Machine

Published On

In this guide we will learn how to fetch or clone private repo in your VM or remote machine. For this guide I am going to use Digital Ocean. Get free 200$ credits for 2 months to try their services.(not sponsored…affiliate link)

There are mainly 3 ways through which you can perform this. I am going to demonstrate the Personal Access Tokens and the SSH approach as it is considered more safe than the email and password approach.

Related Post: Host Node.js App with SSL on Ubuntu Machine using Nginx

Requirements

  • VM
  • github account and private repo

Personal Access Token

This is quite easy to setup.

Go to github developer settings. Then choose the Personal Access Token and then Fine-grained Tokens.

github developer setting

Then tap Generate new token and fill the necessary details.

Give a name so that in future you can recognise it and also expiration date according to you. Now you can select all repo but I personally give access to a particular repo only.

select repo

Now after selecting the repo go the permissions tab and select the Contents to read only.

choose the read only in content permission

Now click on generate token and you will have your token.

choose the read only in content permission

Now copy it and go the vm and move to the directory where you want to clone the repo.

For cloning the private github repo using Personal Access Token

terminal
git clone https://<token>@github.com/<username>/<repository_name>.git
choose the read only in content permission

SSH method

Now if you don’t want to pass the access token everytime you pull code then you can use this SSH method.

Firstly generate the ssh key. In your vm run the following command in the terminal. I am currently in the home folder(user level).

terminal
ssh-keygen -t ed25519 -C "[email protected]"

And tap enter and enter. I am not changing path or anything.

generate ssh key

After the key is generated we have to copy the content of pulic key and paste into the github.

So navigate into the .ssh folder and copy the content of .pub file. Just do the cat command followed by the filename and copy the output.

copy public key

Now go to github settings and in access you will find SSH/GPG keys.

ssh option in access

Go to it and then click on New SSH key. Give a identifiable name. And paste the content of the file you copied above. Click on Add Key and your key will be saved.

paste the public key content

Now you will be able to pull/clone the private repo. Firstly copy the ssh url from the github clone option.

git clone ssh url

Now let’s try cloning the repo.

clone

Conclusion

So now we are able to access and clone the private repo in the remote or Virtual Machine. I personally prefer the Personal Access Token so it give more fine grained control over the repo and permissions.

In this guide I used Digital Ocean. Get free 200$ credits for 2 months to try their services.

If you have any query then please reach me out through EverythingCS discord server. Please fill out the feedback form and tell me how can I improve/