Cloning a GitHub Repo Over HTTPS w/ a Personal Access Token (PAT)

In many organizations, enhanced security protocols including two-factor authentication necessitate the use of a Personal Access Token (PAT) for operations that interact with GitHub remotely via HTTPS. Here's how you can clone a GitHub repository using a PAT.

Cloning a GitHub Repo Over HTTPS w/ a Personal Access Token (PAT)
Photo by Mohammad Rahmani / Unsplash

Generate a Personal Access Token (PAT)

  1. Go to GitHub and sign in.
  2. In the upper-right corner, click your profile photo, then click Settings.
  3. In the left sidebar, click Developer settings.
  4. In the left sidebar, click Personal access tokens.
  5. Click Generate new token.
  6. Give your token a descriptive name, select the scopes or permissions you'd like to grant this token (at a minimum, you will need repo for full control of private repositories), and click Generate token.
  7. Important: Copy your new personal access token. You won’t be able to see it again after you navigate away from the page.

Clone the Repository

  1. Open a terminal on your machine.
  2. Use the following command to clone a repository, replacing username with your GitHub username, repository with the repository name, and TOKEN with your PAT:
git clone https://username:TOKEN@github.com/username/repository.git

Replace username with your GitHub username and repository with the repository name. Insert your PAT where TOKEN is indicated.

Enter the Command

  • Paste the command into your terminal and execute it.
  • This command includes your PAT directly in the URL, which Git will use to authenticate over HTTPS to GitHub.

Security Consideration

Important: Including a PAT in a command can expose it to shell history and other logs. Consider using a credential helper to store your PAT securely or set it as an environment variable in your session to reduce the risk of exposure.

Caching your GitHub credentials in Git - GitHub Docs
If you’re cloning GitHub repositories using HTTPS, we recommend you use GitHub CLI or Git Credential Manager (GCM) to remember your credentials.

Resources

For additional details about GitHub PAT management check out the official docs on GitHub linked on the bookmark below!

Managing your personal access tokens - GitHub Docs
You can use a personal access token in place of a password when authenticating to GitHub in the command line or with the API.