How to Clone a GitHub Private Repository on a Server | Step-by-Step Guide

Cloning a private repository from GitHub to your server is a straightforward process, but it requires a few extra steps compared to a public repository. In this guide, I’ll walk you through the process step-by-step, from generating an SSH key to successfully pulling your repository.
Watch the YouTube Video for a Visual Guide
Step 1: Generate a New SSH Key
First, you’ll need to generate a new SSH key that will be used to authenticate your connection with GitHub.
ssh-keygen -t ed25519 -C "your_email@example.com"
This command will create a new SSH key pair. Make sure to save the key in the default location unless you have a specific reason to store it elsewhere.
Step 2: Add Your SSH Key to the SSH Agent
To ensure your SSH key is readily available, add it to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519Step 3: Add the SSH Key to Your GitHub Account
Next, you’ll need to add the SSH public key to your GitHub account. First, copy the SSH public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
Then, go to GitHub Settings > SSH and GPG keys, click “New SSH key,” and paste your key.
Step 4: Test the SSH Connection
Before proceeding, it’s a good idea to test the SSH connection to GitHub to ensure everything is set up correctly:
ssh -T git@github.com
If everything is configured properly, you’ll receive a success message.
Step 5: Update the Repository Remote URL
Navigate to the folder of your repository on your server and update the remote URL to use SSH:
git remote set-url origin git@github.com:GITHUB_USERNAME/REPO_NAME.gitReplace GITHUB_USERNAME and REPO_NAME with your actual GitHub username and repository name.
Step 6: Pull the Repository
Finally, pull the repository to your server:
git pull origin main
This will fetch the latest version of the code from your private repository.
GitHub Repository: View on GitHub
By following these steps, you can securely clone a private GitHub repository to your server. For a more detailed walkthrough, be sure to check out the YouTube video that accompanies this guide.