Cloning a Git repository using a GitHub token

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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[(https://github.com)] 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:
      Terminal
      git clone https://github.com/username/repository.git
    • To use a token, modify the URL to include your token:
      Terminal
      git clone https://<username>:<token>@github.com/username/repository.git
    • 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.
Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

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:
    Terminal
    git config --global credential.helper cache
  • Using environment variables: Store your token in an environment variable and reference it in your commands:
    Terminal
    export GITHUB_TOKEN="<your_token>"
    git clone https://<username>:${GITHUB_TOKEN}@github.com/username/repository.git
    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.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2