How to clone a private Github Repository

Published on March 27, 2023 · Updated on March 24, 2023
How to clone a private Github Repository

If you’re attempting to clone a private GitHub repository, you may have encountered an error message stating that “support for password authentication was removed on GitHub”. This change was implemented on August 13, 2021, to enhance the security of GitHub users. Instead of using a password, you’ll need to create a personal access token (PAT) to authenticate your account and clone private repositories.

In this article, we’ll go over how to create a PAT on GitHub and configure it in Git for Windows, macOS, and Linux-based operating systems.

Create Personal Access Token on GitHub

  1. Log in to your GitHub account and navigate to your Settings.
  2. Click on Developer settings in the left-hand sidebar, then click on Personal access tokens.
  3. Click on Generate new token and provide a name for the token.
  4. Select the desired scopes (permissions) for the token, then click on Generate token.
  5. Copy the generated token to your clipboard, as you won’t be able to access it again.

Configure PAT in Git

For Windows OS

Search for an application in your Windows OS, named Credential Manager → then Windows Credentials. Search for github.com and edit the password with the token you have generated on GitHub. Now enjoy!

Windows Git Bash

Open Git Bash or Git CMD and run the following command, replacing with your generated token:

git config --global credential.helper wincred

Clone the private repository using the following command, replacing with your GitHub username and with the name of the repository:

git clone https://<your-username>:<your-PAT>@github.com/<your-username>/<repository-name>.git

For MacOS

Click on the Spotlight icon (magnifying glass) on the right side of the menu bar. Type Keychain access then press the Enter key to launch the app → In Keychain Access, search for github.com → Find the internet password entry for github.com → Edit or delete the entry accordingly → You are done

git config --global credential.helper osxkeychain

For Linux-based OS

On Linux you can configure your local GIT client with the following commands:

git config --global user.name "your_github_username"
git config --global user.email "your_github_email"
git config -l

Once GIT is configured, we can begin using it to access GitHub. Example:

git clone https://<your-username>:<your-PAT>@github.com/<your-username>/<repository-name>.git

You can remember the freshly created token in your git cache with:

git config --global credential.helper cache

To delete the cache record you can type the following commands:

git config --global --unset credential.helper
git config --system --unset credential.helper

Conclusion

With the removal of password authentication on GitHub, cloning private repositories now requires a personal access token. By following the steps outlined above, you can create a PAT on GitHub and configure it in Git for your respective operating system.

Load Comments