Skip to content

GitHub Personal Access Token in Visual Studio Code

Problem

GitHub deprecated password authentication for Git operations and now requires token-based authentication. This change affects developers using Visual Studio Code who need to configure their Git operations to use personal access tokens instead of passwords.

Solution Overview

There are several methods to configure GitHub personal access tokens in Visual Studio Code:

  1. Automatic credential prompt - VS Code will prompt for token when needed
  2. Git remote URL update - Directly modify the remote URL to include the token
  3. Git credential manager - Use Git's built-in credential storage
  4. GitHub extension - Use the official VSCode GitHub extension

Step-by-Step Solutions

When you attempt Git operations (push/pull) in VS Code:

  1. Perform a Git operation that requires authentication
  2. VS Code will display a prompt at the bottom: "Signing into GitHub..."
  3. Click the prompt and follow the authentication flow
  4. A browser window will open for GitHub authentication
  5. After successful authentication, you'll be redirected back to VS Code

TIP

If the prompt doesn't appear automatically, try the operation twice or check the Output panel (View → Output) and select "GitHub Authentication" from the dropdown.

Method 2: Update Remote URL with Token

For direct configuration via command line:

  1. Generate a personal access token on GitHub with appropriate permissions
  2. Open VS Code terminal
  3. Run the following command:
bash
git remote set-url origin https://<TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git

Replace:

  • <TOKEN> with your personal access token
  • <USERNAME> with your GitHub username
  • <REPOSITORY> with your repository name

Example:

bash
git remote set-url origin https://ghp_a1b2c3d4e5f6@github.com/username/my-repo.git

Verify the change:

bash
git remote -v

Method 3: Using Git Credential Manager

For systems with Git Credential Manager Core (GCM):

  1. Remove existing credentials:
bash
printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>" | git credential-manager-core erase
  1. Store new token:
bash
printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>\npassword=<newToken>" | git credential-manager-core store

WARNING

Replace credential-manager-core with credential-manager if that's what git config credential.helper returns.

Method 4: macOS Keychain Update

On macOS, update stored credentials:

  1. Open Keychain Access
  2. Search for "github.com"
  3. Find and delete the existing GitHub credential
  4. Next Git operation will prompt for new credentials where you can use your token

Creating a Personal Access Token

  1. Go to GitHub → Settings → Developer Settings → Personal Access Tokens
  2. Click "Generate new token"
  3. Select appropriate scopes (repo, workflow, etc.)
  4. Generate and securely save the token

DANGER

Tokens have sensitive permissions. Never commit them to version control or share them publicly.

Troubleshooting

Common Issues

  1. No authentication prompt appears:

    • Check if credentials are cached (use git credential-manager-core get)
    • Ensure you have the latest Git and VS Code versions
  2. Authentication fails:

    • Verify token has correct permissions
    • Check token hasn't expired
  3. On Linux:

    • Install Git Credential Manager Core if not present
    • Ensure you're using a recent Git version

Verifying Configuration

Check your remote configuration:

bash
git remote -v

The output should show your remote URLs. If using Method 2, they should contain your token.

Best Practices

  1. Use token with minimal required permissions
  2. Store tokens securely - use credential managers instead of hardcoding in URLs
  3. Rotate tokens regularly for security
  4. Use SSH as an alternative if preferred (generate SSH keys instead of tokens)

Alternative: SSH Authentication

Consider using SSH keys instead of personal access tokens:

bash
# Generate SSH key
ssh-keygen -t ed25519 -C "your_email@example.com"

# Add to ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# Add public key to GitHub account

Then use SSH URLs for your remotes:

bash
git remote set-url origin git@github.com:username/repository.git

This approach eliminates the need for token management in many cases.

INFO

Both token-based HTTPS authentication and SSH keys are valid approaches. Choose based on your security requirements and workflow preferences.

By following these methods, you can ensure uninterrupted Git operations in Visual Studio Code while complying with GitHub's authentication requirements.