- Published on
GitHub Authentication: Generating and Using Personal Access Tokens
4 min read
- Authors
- Name
- Christopher Morales
- @
Table of Contents
In this guide, we'll cover the process of generating a GitHub personal access token (PAT) and using it for secure authentication with Git. This method replaces the now-deprecated password authentication for Git operations over HTTPS.
What is a Personal Access Token?
A Personal Access Token (PAT) is a secure alternative to using a password for GitHub authentication. It provides fine-grained control over what the token can access and is more secure than using your GitHub password for tasks such as cloning repositories, pushing changes, and accessing APIs.
Step 1: Generating a Personal Access Token
Follow these steps to generate a personal access token:
Log in to GitHub
- Navigate to GitHub and log in to your account.
Access Token Settings
- Click on your profile picture in the top-right corner and select Settings.
- In the left sidebar, click Developer settings > Personal access tokens > Tokens (classic).
Create a New Token
- Click on Generate new token (or Generate new token (classic) if using classic tokens).
- Provide a descriptive name for the token (e.g., "Docker Pterodactyl Minecraft Server").
Select Scopes
- Choose the appropriate scopes for your use case. For basic Git operations, select:
repo
: Full control of private repositories.workflow
: Access to GitHub Actions workflows (optional).
- Scroll down and click Generate token.
- Choose the appropriate scopes for your use case. For basic Git operations, select:
Copy the Token
- Once generated, copy the token immediately as you won't be able to view it again. Store it securely (e.g., in a password manager).
Generate a Token in the GitHub CLI (Optional)
- If you prefer to use the GitHub CLI, run:
gh auth login
- Follow the prompts to authenticate and generate a token directly in your terminal.
- If you prefer to use the GitHub CLI, run:
Step 2: Using the Token for Authentication
Configuring Git to Use the Token (Option A)
Replace HTTPS Password with Token
- When performing Git operations (e.g.,
git clone
,git push
), GitHub will prompt for your username and password. - Use your GitHub username as the username.
- Paste the personal access token as the password.
- When performing Git operations (e.g.,
Cache the Token Locally
- To avoid entering the token repeatedly, configure Git to store credentials:
git config --global credential.helper store
- To avoid entering the token repeatedly, configure Git to store credentials:
Update Repository Remote URL (Optional)
- Ensure your repository is set to use HTTPS:
git remote set-url origin https://github.com/<username>/<repository>.git
- Ensure your repository is set to use HTTPS:
Alternative Solution: Using SSH Keys (Option B)
If you prefer SSH-based authentication instead of personal access tokens, follow these steps to set up SSH keys:
Generate an SSH Key
- Run the following command in your terminal:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Press Enter to accept the default file location (e.g.,
~/.ssh/id_rsa
). - (Optional) Set a passphrase for additional security.
- Run the following command in your terminal:
Add the SSH Key to GitHub
- Copy the public key to your clipboard:
cat ~/.ssh/id_rsa.pub
- Log in to GitHub and navigate to Settings > SSH and GPG keys.
- Click New SSH key, paste the key, and save.
- Copy the public key to your clipboard:
Test the Connection
- Test your SSH key with GitHub:
ssh -T git@github.com
- You should see a success message.
- Test your SSH key with GitHub:
Update Repository Remote URL
- Update your repository to use SSH:
git remote set-url origin git@github.com:<username>/<repository>.git
- Update your repository to use SSH:
Step 3: Securing and Managing Tokens
Token Expiry
- Set an expiration date for your token during creation for added security. Regenerate the token as needed.
Revoking Tokens
- If your token is compromised or no longer needed, revoke it from the Personal access tokens page in your GitHub settings.
Storing Tokens
- Use a secure method to store tokens, such as a password manager or environment variables.
Example of storing the token as an environment variable:
export GITHUB_TOKEN=<your_token>
Using Environment Variables in Scripts
- Reference the token in scripts:
git clone https://$GITHUB_TOKEN@github.com/<username>/<repository>.git
- Reference the token in scripts:
Conclusion
Using a personal access token is a secure and effective way to authenticate with GitHub. Alternatively, SSH keys provide a robust option for managing secure connections. By following this guide, you'll be able to manage your repositories seamlessly while maintaining security.
For more details, visit the GitHub Documentation.