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:
- Automatic credential prompt - VS Code will prompt for token when needed
- Git remote URL update - Directly modify the remote URL to include the token
- Git credential manager - Use Git's built-in credential storage
- GitHub extension - Use the official VSCode GitHub extension
Step-by-Step Solutions
Method 1: Automatic Authentication (Recommended)
When you attempt Git operations (push/pull) in VS Code:
- Perform a Git operation that requires authentication
- VS Code will display a prompt at the bottom: "Signing into GitHub..."
- Click the prompt and follow the authentication flow
- A browser window will open for GitHub authentication
- 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:
- Generate a personal access token on GitHub with appropriate permissions
- Open VS Code terminal
- Run the following command:
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:
git remote set-url origin https://ghp_a1b2c3d4e5f6@github.com/username/my-repo.git
Verify the change:
git remote -v
Method 3: Using Git Credential Manager
For systems with Git Credential Manager Core (GCM):
- Remove existing credentials:
printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>" | git credential-manager-core erase
- Store new token:
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:
- Open Keychain Access
- Search for "github.com"
- Find and delete the existing GitHub credential
- Next Git operation will prompt for new credentials where you can use your token
Creating a Personal Access Token
- Go to GitHub → Settings → Developer Settings → Personal Access Tokens
- Click "Generate new token"
- Select appropriate scopes (repo, workflow, etc.)
- Generate and securely save the token
DANGER
Tokens have sensitive permissions. Never commit them to version control or share them publicly.
Troubleshooting
Common Issues
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
- Check if credentials are cached (use
Authentication fails:
- Verify token has correct permissions
- Check token hasn't expired
On Linux:
- Install Git Credential Manager Core if not present
- Ensure you're using a recent Git version
Verifying Configuration
Check your remote configuration:
git remote -v
The output should show your remote URLs. If using Method 2, they should contain your token.
Best Practices
- Use token with minimal required permissions
- Store tokens securely - use credential managers instead of hardcoding in URLs
- Rotate tokens regularly for security
- 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:
# 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:
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.