How to add a GitHub personal access token to Visual Studio Code

GitGithubVisual Studio-CodeAccess Token

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

Follow these simple steps to set up GitHub authentication with a personal access token:

  1. Open a command line window on your PC or Terminal on Mac

  2. Set the current directory to your project root

    cd C:\Users\Giddy\source\repo\MySampleProject
    
  3. 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 - Git

Tested on Visual Studio Code (Mac)

> no need for an extra extension. I only trust official extensions, sorry. > GitHub extension by KnisterPeter

  1. Generate a personal access token from github.com
  2. Make sure to save your access token (e.g., ghp_pVC*****)
  3. Open your project with Visual Studio Code or navigate to your project in the terminal, cd ~/path/to/your/project
  4. 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
  5. Now you can try git push
Note:

When generating a personal access token, make sure to enable workflow:

Enter image description here

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 - Git

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 > > Enable Visual Studio Code GitHub Support

  1. Go to SettingsDeveloper SettingsPersonal Access Token

    Or go directly to https://github.com/settings/tokens

  2. Click Generate New Token

    Settings -> Generate New Token

  3. Go back to Visual Studio Code and open the Command Palette (Ctrl + Shift + P)

  4. Type in GitHub: Set Personal Access Token

    Set Personal Access Token

  5. Paste in the value from your newly generated token

Solution 4 - Git

  1. In Visual Studio Code, try to push or pull from the remote repository
  2. Click the 'Signing into GitHub' message at the bottom of Visual Studio Code
  3. Click 'Allow' in the prompt
  4. Enter the token in the prompt that appears at the top of Visual Studio Code

To see the output related to GitHub authentication:

  1. Open a terminal in Visual Studio Code
  2. Click 'Output' at the top of the terminal panel
  3. Click 'GitHub Authentication' from the drop down menu

At the time of writing, I'm on Visual Studio Code v1.56.0.

Solution 5 - Git

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.

Updating credentials from the macOS Keychain

Solution 6 - Git

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):

  1. 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
  2. Set up Personal Access Token with instructions on GitHub
  3. Go into the Mac keychain and delete the vscode.github.com password credential
  4. Install Microsoft's Git Credential Manager Core (GCMC) - instructions on GitHub
  5. 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 - Git

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 8 - Git

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 9 - Git

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 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1186050View Question on Stackoverflow
Solution 1 - GitGiddy NayaView Answer on Stackoverflow
Solution 2 - GitsultanmyrzaView Answer on Stackoverflow
Solution 3 - GitKyleMitView Answer on Stackoverflow
Solution 4 - GitChris HowardView Answer on Stackoverflow
Solution 5 - GitMohammed TaherView Answer on Stackoverflow
Solution 6 - GitsnowboundView Answer on Stackoverflow
Solution 7 - GitVonCView Answer on Stackoverflow
Solution 8 - GitnadirView Answer on Stackoverflow
Solution 9 - GitJesseView Answer on Stackoverflow
Solution 10 - GitTerry SnowView Answer on Stackoverflow