Posted on • Updated on

How to Clone Github Private Repo To Any Linux Server

author

Kai

Step 1 -  Generating a new SSH key

If you have not already configured one, run the following command to generate an SSH key:

$ ssh-keygen -t rsa -f ~/.ssh/my_key -b 4096 -C "username@example.com"

Replace my_key with your key name.

Copy the content of my_key.pub for future use.

$ cat ~/.ssh/my_key.pub

Step 2 - Adding your SSH key to the ssh-agent

# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
> Agent pid XXXXX
$ ssh-add ~/.ssh/my_key

Step 3 - Adding an SSH key to your GitHub Project Repository

  • Go to the repository(e.g. @username/my-laravel-app).
  • Select the settings tab.
  • Go to Deploy keys under Security
  • Click on Add Deploy Key (Top right section).
  • Give a title to your deploy key and paste the content of (my_key.pub) in the key section.
  • You can check Allow write access. If you want this key to be used to push to this repository? Deploy keys always have pull access. 
  • Click on Add key to save Deploy Key.

Step 4 - Test the SSH key

$ ssh -i ~/.ssh/my_key -T git@github.com

//output

Hi username/my-laravel-app! You've successfully authenticated, but GitHub does not provide shell access.

Step 5 - Setup SSH config.

$ nano ~/.ssh/config

Refer to the following config to set up.

#Github repo1
Host my-laravel-app
        HostName github.com
        User git
        IdentityFile ~/.ssh/my_key
                IdentitiesOnly yes

To Save CTRL+O | ENTER | CTRL+X

Test SSH config 

$ ssh {Config Host} -T
$ ssh my-laravel-app -T

//output similar to the previous test (Step 4)

Hi username/my-laravel-app! You've successfully authenticated, but GitHub does not provide shell access.

Step 6 - Clone your repository

#cd into your directory where you want to clone your gitHubrepository
$ cd ~/repositories
$ git clone Host:username/repo.git
$ git clone my-laravel-app:username/my-laravel-app.git

Clone from a specific branch :

$ git clone -b <branch_name> <remote_repo_url>
$ git clone -b production my-laravel-app:username/my-laravel-app.git

Here -b is just an alias for --branch

Step 7 - cd into the project and install required dependencies (if any)

$ cd my-laravel-app

💡 Check out our quick and easy guide on how to deploy and set up your Laravel project. 

end of article

Related Posts

Join Our Newsletter

Want the latest & greatest from our blog straight to your inbox with some exclusive offers from our partners and sponsors?

We won't spam. Promise.