Overview
Personal Access Tokens (PATs) act like passwords and are used to authenticate against APIs, Git operations, and tools like GitHub CLI or Azure DevOps pipelines. This post walks you through creating PATs in both platforms so the user can interact with both repositories securely.
β GitHub: Creating a Personal Access Token
πΉ Step 1: Go to Developer Settings
- Login to GitHub at https://github.com.
- Click on the profile picture (top right corner) β Settings.
- Scroll down the left sidebar and click Developer settings.
- Under βDeveloper settings,β click Personal access tokens > Tokens (classic) or Fine-grained tokens.
π GitHub recommends Fine-grained tokens for most use cases. These provide more control and are more secure than classic tokens.
πΉ Step 2: Generate the token
Option A: Fine-grained token (recommended)
- Click Generate new token β Fine-grained token.
- Name the token (e.g., My PAT Token).
- Set an expiration (e.g., 90 days).
- Choose the repository access:
- Select Only select repositories or All repositories depending on your needs.
- Select permissions (e.g.,
Contents: Read and Write
if pushing code). - Click Generate token.
- Copy the token and store it securely (e.g., in a password manager).
π Please note that GitHub wonβt show it again. If the user loses it, they must generate a new one.
Option B: Classic token
- Click Generate new token (classic).
- Fill in token name, expiration, and select scopes (e.g.,
repo
,workflow
,read:org
, etc.). - Click Generate token and copy it.
β How to Use the GitHub PAT
You can use this token:
-
As a password when running
git clone
,git push
, orgit pull
:git clone https://github.com/your-username/your-repo.git Username: your GitHub username Password: your PAT
-
In CI/CD tools, scripts, and GitHub CLI (
gh
)
π· Azure DevOps: Creating a Personal Access Token
πΉ Step 1: Open user settings
- Login to Azure DevOps at https://dev.azure.com.
- In the top-right corner, click on the user profile picture.
- Select Personal access tokens.
πΉ Step 2: Generate a new token
- Click + New Token.
- Enter a name (e.g., Developer Portal PAT).
- Set organization, expiration (30, 60, 90 days, or custom).
- Choose Scopes:
- Common examples:
- Code (Read & Write) to clone/push/pull repositories.
- Build (Read & Execute) for pipelines.
- Work Items (Read & Write) for DevOps boards integration.
- Common examples:
- Click Create.
- Copy the token and store it in a safe place.
β How to Use the Azure DevOps PAT
You can use this token:
-
With Git commands:
git clone https://dev.azure.com/your-org/your-project/_git/your-repo Username: anything (can be blank) Password: your PAT
-
In build pipelines, secret variables, or integration tools (like Postman)
π Best Practices for PATs
- Never commit a PAT to source control.
- Use fine-grained permissions where possible.
- Set short expiration periods and regenerate when needed.
- Store securely in tools like Azure Key Vault, GitHub Secrets, or password managers.
- Revoke tokens if they are no longer needed or compromised.