Cloning a Git repository lets you copy a project’s entire codebase to your local machine. While cloning public repositories is straightforward, cloning private repositories requires authentication because the code isn’t publicly accessible.
In this guide, you’ll learn how to securely clone private Git repositories from platforms like GitHub, GitLab, or Bitbucket.
Why Is a Private Repository Different?
Private repositories restrict access to authorized users only. To clone such a repo, you need to prove your identity to the hosting service, typically via:
- Username and password / personal access token (PAT)
- SSH keys
Prerequisites
- Access permissions to the private repository
- Git installed on your computer (Download Git)
- An SSH key configured with your Git provider (optional but recommended)
Method 1: Cloning Using HTTPS with Personal Access Token (PAT)
Step 1: Generate a Personal Access Token
Most services like GitHub no longer support password authentication for Git operations over HTTPS. Instead, generate a PAT:
- On GitHub, go to Settings > Developer settings > Personal access tokens
- Create a token with appropriate scopes (e.g.,
repo
for full repo access) - Copy the token (you won’t see it again!)
Step 2: Clone the Repository
Use the HTTPS URL, replacing your-username
and repo-name
:
git clone https://github.com/your-username/repo-name.git
When prompted for a password, enter your Personal Access Token instead of your GitHub password.
Method 2: Cloning Using SSH Keys (Recommended)
SSH keys provide a secure and convenient way to authenticate without entering credentials each time.
Step 1: Generate an SSH Key (if you don’t have one)
ssh-keygen -t ed25519 -C "yo********@ex*****.com"
Or, for older systems:
ssh-keygen -t rsa -b 4096 -C "yo********@ex*****.com"
Follow the prompts to save the key.
Step 2: Add Your SSH Key to Your Git Provider
- Copy your public key:
cat ~/.ssh/id_ed25519.pub
- Add the key to GitHub under Settings > SSH and GPG keys > New SSH key
Step 3: Clone Using SSH
Use the SSH URL from the repo page:
git clone gi*@gi****.com:your-username/repo-name.git
Since your SSH key is already authorized, cloning will proceed without asking for a password.
Troubleshooting Tips
- Permission denied errors? Double-check that your SSH key is added to your Git account and your local SSH agent is running (
ssh-agent
). - Ensure correct URL: Confirm you’re using the SSH or HTTPS URL matching your authentication method.
- Check access rights: Verify you have permission to access the private repository.
Summary
Authentication Method | Command Example | Notes |
---|---|---|
HTTPS + PAT | git clone https://github.com/username/repo.git | Enter PAT as password |
SSH Key | git clone gi*@gi****.com:username/repo.git | No password prompts after setup |
Final Thoughts
Cloning private repositories is easy once authentication is set up. Using SSH keys is generally recommended for security and convenience. Always protect your access tokens and private keys.