How to clone a private repository from GitHub

Cloning a repository makes it easy to work on projects. It enhances editions, pull requests, merge requests, and other activities.

There are two types of repositories:

  • Public repository: A repository accessible to everyone online.
  • Private repository: A repository that is accessible to the owner and developers that are given access. A private repository owned by an organization is accessible to its members.

Cloning a public repository is an easy task. The Answer how to clone a git repository explains this in detail.

In contrast to a public repository, cloning a private repository can be confusing and needs advanced understanding. The following explains how a developer can clone a remote repository from GitHub.

Clone a private repository

Cloning a private repository can be a little bit tricky and is only possible if these conditions are met:

  • The cloner is cloning from a personal account.
  • The repository is from an organization of which the cloner is a member.
  • The repository is neither from a personal account nor an organization of which the cloner is a member, but the owner has given the cloner access to the repository.

Once these conditions are met, anyone can clone a private repository in the following ways:

1. Using HTTPS with the username and password

To clone a private repository using HTTPS with a username and password, follow these steps:

  1. Go to the private repository.
  2. Click the “Code” button and copy the link.
Copy repo HTTPS link
Copy repo HTTPS link
  1. Use this command to clone the repository.

git clone https://username:password@github.com/yourproject.git

Note: A username is the cloner’s GitHub username, and a password is their password.

Alternatively, the command could be like this.


git clone https://username@github.com/yourproject.git

This command will prompt the cloner to enter their account’s password.

Using this method is only recommended for a personal project that does not involve contributors as anyone with the link will see the username and password.

2. Using PAT(Personal Access Token)

Follow the steps below to clone a private repository from a personal account or account of an organization.

  1. Go to “Settings”.
Click the "Settings" option from the drop-down menu
Click the "Settings" option from the drop-down menu
  1. Once the page loads, scroll to the bottom and click the “Developer Settings” option.
Select the "Developer settings" option
Select the "Developer settings" option
  1. Click the “Personal access tokens” option, choose the “Token (classic)” option, then click the “Generate new token” option.
Click the "Generate new token" option
Click the "Generate new token" option
  1. Fill in the required input boxes and choose the “All repository” option.
Click the "All repositories" option
Click the "All repositories" option
  1. Click the “Generate token” option to get the token.
The token
The token
  1. Copy the token generated and use it with this command.

git clone https://<pat>@github.com/<your account or organization>/<repo>.git

Replace with the PAT generated, with the private repository owner’s account username, and with the repository name.

Finally, a private repository will be cloned.

Copyright ©2024 Educative, Inc. All rights reserved