Cloning a Git repository using a GitHub token

Greg Foster
Greg Foster
Graphite software engineer

When working with private repositories or within organizations that enforce strong security protocols, you may need to use a personal access token (PAT) for authentication during operations like cloning a repository.

A personal access token can provide more security than traditional password-based authentication, especially after many platforms, including GitHub, have moved away from supporting password authentication for Git operations.

This guide details the process of using a token to clone a repository using Git.

A personal access token (PAT) is a user-generated token that can be used in place of a password with command line tools, APIs, and other applications to ensure a higher level of security. Tokens can be configured to expire and to provide specific access permissions to various aspects of your repositories.

Before you can clone a repository using a token, you need to generate one. As an example here’s how you can generate one in GitHub as an example:

  1. Log in to GitHub: Navigate to the [GitHub web UI[(] and sign into your account.
  2. Access token settings:
    • Go to your profile settings.
    • Click on "Developer settings."
    • Select "Personal access tokens."
    • Click "Generate new token."
  3. Set up your token:
    • Give your token a descriptive name.
    • Select the scopes or permissions you want the token to have. For cloning a repository, you might only need "repo" access.
    • Optionally set an expiration date.
    • Click "Generate token."
  4. Copy your new token: Ensure you copy your new token now; you won’t be able to see it again once you navigate away from the page.

Once you have your personal access token, you can use it to clone a repository from the command line:

  1. Open your terminal
  2. Prepare the repository URL:
    • Normally, you might clone a repository with a command like:
      git clone
    • To use a token, modify the URL to include your token:
      git clone https://<username>:<token>
    • Replace <username> with your GitHub username and <token> with your personal access token.
  • Security: Never share your personal access token. Treat it like a password. If you think your token has been compromised, revoke it immediately and generate a new one.
  • Use HTTPS URLs: Make sure you use HTTPS URLs for cloning with tokens. SSH URLs won’t work with personal access tokens.
  • Store tokens securely: Consider using a secure vault for storing your personal access tokens, especially if you work with multiple tokens or sensitive data.

If you frequently need to use personal access tokens with Git, consider automating your setup:

  • Caching your credentials: You can cache your token using Git's credential helper:
    git config --global credential.helper cache
  • Using environment variables: Store your token in an environment variable and reference it in your commands:
    export GITHUB_TOKEN="<your_token>"
    git clone https://<username>:${GITHUB_TOKEN}
    Note: While this works as a temporary solution, caution should be used when storing credentials in environment variables as they are stored locally in plaintext. This is considered unsafe and this method should only be used in one-off situations where the env variable is immediately reset upon completion of the command.

For further reading on using personal access tokens, see the official GitHub documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2