How to Clone a Private Repository in Git

Azhar Bashir Khan Feb 02, 2024
How to Clone a Private Repository in Git

This tutorial will teach you to clone a private repository hosted on Github using Git.

Git is a version control system utilized to track changes in a project directory. Git uses commits for such purposes.

Github provides Internet hosting for software development and version control using Git. We can have both public and private Git repositories on Github.

We can clone private repositories hosted on Github using the correct credentials. We will now illustrate this with an example.

Clone a Private Repository in Git

To clone a public repository hosted on Github, we need to run the git clone command as shown below.

$ git clone https://github.com/d2l-ai/berkeley-stat-157.git

This clones the public repository into a new directory that is created. Also, it creates the remote-tracking branches for each branch in the cloned repository.

It then creates and checks out an initial branch that is forked from the current active branch of the repository.

To clone a private repository hosted on Github, we need the proper credentials.

Typically, on Github, we can have two-factor authentication enabled. Two-factor authentication (2FA) gives an extra layer of security when logging into websites or apps.

When the two-factor authentication (2FA) is enabled, we have to provide the username and password and another form of authentication that only we know or can access.

Thus, upon enabling two-factor authentication (2FA), we must provide a Personal Access Token (PAT) instead of the password when cloning private repositories using the HTTPS URLs.

Thus, first, we need to create a Personal Access Token (PAT) on Github.

To create the Personal Access Token (PAT) on Github, we need to follow the steps given here, viz, Creating a personal access token.

After creating the Personal Access Token (PAT), we can now clone the private repository using the git clone command.

Suppose we have an account on Github with the username johndoe. We also have to create a Personal Access Token (PAT) for this account on Github.

We have a private repository with the name My_Project on Github. To clone the private repository My_Project on Github, we need to execute the git clone command.

$ git clone https://github.com/johndoe/My_Project.git
Cloning into 'My_Project'...
Username for 'https://github.com': johndoe
Password for 'https://johndoe@github.com':

When prompted upon executing the git clone command, we must enter the username johndoe. For the password, instead of the Github login password, we need to enter the personal access token (PAT) (that we had created for such purpose).

Upon authenticating the credentials, the git clone command will start fetching and cloning the My_Project Git repository into our local system.

We can view our Personal Access Tokens (PATs) on Github at this location, viz, Github Personal access tokens.

Thus, we have learned how to clone a private repository hosted on Github using Git.

For more information, please visit the following sources.

  1. git-clone
  2. git clone
  3. Creating a personal access token
  4. About two-factor authentication
  5. Cloning with HTTPS URLs

Related Article - Git Clone