'How to add a GitHub personal access token to Visual Studio Code
I received an email saying GitHub will require token authentication after August 13 2021. I want to ensure I don't have an interruption of service (push/pull) after this date. So I logged into GitHub and created a token for my single repository.
Now I want to use the token to push/pull my repository from GitHub, in Visual Studio Code, which uses Git and the command line, which I have installed on my Mac.
What do I do to add/replace the password from GitHub with the generated token I just created to push/pull from my repository? Can I do it from Visual Studio Code or does it get added from the terminal command line?
Solution 1:[1]
Follow these simple steps to set up GitHub authentication with a personal access token:
Open a command line window on your PC or Terminal on Mac
Set the current directory to your project root
cd C:\Users\Giddy\source\repo\MySampleProject
Run the command to set remote access via a token
git remote set-url origin https://username:[email protected]/username/repository.git
Example:
git remote set-url origin https://sampleuser:a7b19929***[email protected]/sampleuser/sampleproject.git
Solution 2:[2]
Tested on Visual Studio Code (Mac)
no need for an extra extension. I only trust official extensions, sorry. GitHub extension by KnisterPeter
- Generate a personal access token from github.com
- Make sure to save your access token (e.g., ghp_pVC*****)
- Open your project with Visual Studio Code or navigate to your project in the terminal,
cd ~/path/to/your/project
- In the Visual Studio Code terminal,
git remote set-url origin https://<personal_access_token>@github.com/<your_username or organization_name>/<repo_name>.git
- Now you can try
git push
Note:
When generating a personal access token, make sure to enable workflow:
Hint
You can type git remote -v
to see your origin or upstream.
origin https://github.com/<username>/<repo_name>.git (fetch)
origin https://github.com/<username>/<repo_name>.git (push)
upstream https://github.com/<username>/<repo_name>.git (fetch)
upstream https://github.com/<username>/<repo_name>.git (push)
Also after setting git remote set-url origin https://<personal_access_token>@github.com/<your_username>/<repo_name>.git
Your git remote -v
should be something like:
origin https://<your_personal_access_token>@github.com/<username>/<repo_name>.git (fetch)
origin https://<your_personal_access_token>@github.com/<username>/<repo_name>.git (push)
Solution 3:[3]
If you get a message like this using the GitHub extension by KnisterPeter:
To enable the Visual Studio Code GitHub Support, please set a Personal Access Token
Go to Settings ? Developer Settings ? Personal Access Token
Or go directly to https://github.com/settings/tokens
Click Generate New Token
Go back to Visual Studio Code and open the Command Palette (Ctrl + Shift + P)
Type in GitHub: Set Personal Access Token
Paste in the value from your newly generated token
Solution 4:[4]
- In Visual Studio Code, try to push or pull from the remote repository
- Click the 'Signing into GitHub' message at the bottom of Visual Studio Code
- Click 'Allow' in the prompt
- Enter the token in the prompt that appears at the top of Visual Studio Code
To see the output related to GitHub authentication:
- Open a terminal in Visual Studio Code
- Click 'Output' at the top of the terminal panel
- Click 'GitHub Authentication' from the drop down menu
At the time of writing, I'm on Visual Studio Code v1.56.0.
Solution 5:[5]
If you are not prompted for your username and password, your credentials may be cached on your computer. You can update your credentials in the Keychain to replace your old password with the token.
Solution 6:[6]
I was on GitHub password authentication, and got hit with Visual Studio Code's "you can't use password auth for GitHub anymore".
I was on a Mac, so I had the stock Git (v2.21).
I tried these steps and can now check in code via Visual Studio Code error free (note this approach uses Microsoft Git Credential Manager Core - see here):
- Update Git using Homebrew,
brew install git
. It'll create a new symbolic link on your system to Git pointing to /usr/local/bin/git, so the Mac stock Git won't be used - Set up Personal Access Token with instructions on GitHub
- Go into the Mac keychain and delete the
vscode.github.com
password credential - Install Microsoft's Git Credential Manager Core (GCMC) - instructions on GitHub
- Quit and restart Visual Studio Code, and push again. You'll be prompted to authorise GCMC in a browser. Once you do that, pushing from Visual Studio Code should work automatically as the GCMC will handle Git authorisation going forward.
Solution 7:[7]
Copy the following line and paste it into your Visual Studio Code terminal
git remote set-url origin https://<TOKEN>@github.com/<USERNAME>/<REPOSITORYNAME>.git
Replace TOKEN with your token USERNAME with username and REPOSITORYNAME with your repository. This should work. Your final line should look something like:
git remote set-url origin https://[email protected]/brother/sampleapp.git
Solution 8:[8]
If you are using HTTPS URLs for your remote repositories, that means you can cache your credentials: today, that would be your GitHub user account name and password, tomorrow, the password will be your token.
Check your git config credential.helper
result.
If it is manager or manager-core, remove the old password with a git credential-manager-core erase
("destructive command" in that it will remote the github.com
entry and its associated value, the password)
(a git credential-manager-core get
would read that old value)
printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>"|git credential-manager-core erase
Then git credential-manager-core store
to store the token:
printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>\npassword=<newToken>"|git credential-manager-core store
(replace credential-manager-core
by credential-manager
if the credential helper is manager
instead of manager-core
)
Visual Studio Code will use that credential helper, with the new "password" (token) stored for the remote site.
If you get, on Linux:
git: 'credential-manager-core' is not a git command. See 'git --help'.
You would need to to download and install GCM.
Solution 9:[9]
I had the same issue and finally got the 'Signing into GitHub' prompt to appear and authenticate successfully. Basically what you need to do after getting the confirmation page below (after you click 'Authorize Visual Studio Code' in the browser window):
Success!
Authorization was successful. You will be redirected back to Visual Studio Code
Didn't work? If you aren't redirected, you can add the token manually.
**Your authorization token:**
vscode://vscode.github-authentication/did-authenticate?windowid=1&code=[Not-A+Real+Token)SJ###AASDf
......... etc
Is switch back to VS Code (probably already came into focus automatically) and choose 'Do Not Prompt Again' or whatever the message is... THENNN you can finally see and click the prompt 'Signing into GitHub ...'
Hopefully this helps someone... because as simple as it sounds, VS Code would not prompt when closing the prompt nor when choosing the option to 'Show Git Log'.
Solution 10:[10]
I found that I was able to solve this by signing out of GitHub then signing back in. I was getting the error "No remote repository" in Visual Studio 2017 in Windows 10, when there was clearly a repository on GitHub. On the same PC, I use GitHub Desktop for python projects in Spyder, on the same GitHub account, and I was not having access errors, and I was not using Personal Access Tokens, so that pointed to an issue with the Visual Studio Connection.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow