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.
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.
Now after selecting the repo go the permissions tab and select the Contents to read only.
Now click on generate token and you will have your token.
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
git clone https://<token>@github.com/<username>/<repository_name>.git
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).
ssh-keygen -t ed25519 -C "[email protected]"
And tap enter and enter. I am not changing path or anything.
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.
Now go to github settings and in access you will find SSH/GPG keys.
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.
Now you will be able to pull/clone the private repo. Firstly copy the ssh url from the github clone option.
Now let’s try cloning the repo.
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/