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:
- Go to the private repository.
- Click the “Code” button and copy the link.
- 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.
- Go to “Settings”.
- Once the page loads, scroll to the bottom and click the “Developer Settings” option.
- Click the “Personal access tokens” option, choose the “Token (classic)” option, then click the “Generate new token” option.
- Fill in the required input boxes and choose the “All repository” option.
- Click the “Generate token” option to get the token.
- Copy the token generated and use it with this command.
git clone https://<pat>@github.com/<your account or organization>/<repo>.git
Replace
Finally, a private repository will be cloned.
Free Resources