83

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chuckd
  • 13,460
  • 29
  • 152
  • 331

11 Answers11

125

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:token@github.com/username/repository.git
    

    Example:

    git remote set-url origin https://sampleuser:a7b19929***58fe65d1c@github.com/sampleuser/sampleproject.git
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Giddy Naya
  • 4,237
  • 2
  • 17
  • 30
50

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)
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
15

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • 12
    "No matching commands" when searching for "Github: Set Personal Access Token" – Amar Ilindra Sep 28 '21 at 13:46
  • 3
    @AmarIlindra - you'll need the [Github extension](https://marketplace.visualstudio.com/items?itemName=KnisterPeter.vscode-github) installed - if you have it installed, and still don't see it, I'd file an issue there – KyleMit Sep 28 '21 at 14:49
  • 2
    @KyleMit That is not an official extension – mr.loop Oct 14 '21 at 07:44
  • @mr.loop, sure, but it's a common one that might have brought people to this Q&A anyway since it has the same debug error message – KyleMit Oct 14 '21 at 12:47
  • 5
    Not trusting a third party with my access tokens. That's just bad form. – Bus42 Feb 14 '22 at 18:50
  • 1
    This extension is deprecated in favour of [Github Pull Requests and Issues] (https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github). As noted by KnisterPeter – Rob Von Nesselrode Jun 09 '23 at 21:20
3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I ran your command and I see 'osxkeychain' – chuckd Feb 17 '21 at 01:11
  • 2
    @user1186050 For Mac, you can replace `credential-manager-core` by `credential-osxkeychain` (https://stackoverflow.com/a/28007050/6309) Or you can do it graphically: https://docs.github.com/en/github/using-git/updating-credentials-from-the-macos-keychaina – VonC Feb 17 '21 at 07:45
  • 1
    I ran `git config credential.helper` and got nothing.. – Ben Whitmore May 11 '21 at 11:17
  • @BenWhitmore What version of Git and OS are you using? – VonC May 11 '21 at 11:52
  • @VonC git 2.25.1 on Ubuntu 20.04.2 LTS – Ben Whitmore May 13 '21 at 06:55
  • @BenWhitmore Is it possible to upgrade to Git 2.31.1? and then install https://github.com/microsoft/Git-Credential-Manager-Core, then install libsecret and set `credential.credentialStore` to `secretservice`, as described in https://github.com/microsoft/Git-Credential-Manager-Core/blob/master/docs/linuxcredstores.md#1-freedesktoporg-secret-service-api, and discussed in https://stackoverflow.com/a/51505417/6309. – VonC May 13 '21 at 12:19
  • Seems like a lot of bother. GiddyNaya's answer worked for me. – Ben Whitmore May 17 '21 at 01:48
  • 1
    @BenWhitmore you mean putting a password or token in clear right in the URL? That is a bad idea, based on a bad answer. – VonC May 17 '21 at 04:47
  • On Linux ([Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa), but with [Cinnamon](https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment)). And a working Git / push to GitHub ***before*** the update to Visual Studio Code 1.62.2), for the first command line, I got `git: 'credential-manager-core' is not a git command. See 'git --help'.` and `git: 'credential-manager' is not a git command. See 'git --help'`. Git version: 2.25.1 – Peter Mortensen Nov 16 '21 at 19:29
  • 1
    Perhaps mention in the answer if the first command is (really) destructive or not (`erase`)? – Peter Mortensen Nov 16 '21 at 19:31
  • @PeterMortensen Good idea. I have (actively) edited the answer accordingly. – VonC Nov 16 '21 at 20:44
  • @PeterMortensen And I have added the "download and install" link for GCM. – VonC Nov 16 '21 at 21:18
  • Windows cmd.exe -- https://github.com/GitCredentialManager/git-credential-manager/issues/634 – irvnriir Mar 19 '22 at 09:10
3
  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    Had a different flow after the 1st step, but similar overall (VSCode v1.56.1, Windows). Got confused by the auth prompt asking for a "password" when it really wanted the the token though (I entered my GitHub password at first) – grunet May 24 '21 at 14:29
  • 2
    i get no such prompt – Capagris Jun 30 '21 at 17:01
  • 8
    when I try 'git push', I never see 'Signing into GitHub' message, all I see is 'remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information. fatal: unable to access 'https://github.com/ivanpartizan/algorithms.git/': The requested URL returned error: 403' I created my Personal Access Token, but how can I use it??? – ivan milenkovic Aug 14 '21 at 01:30
  • @ivanmilenkovic see: https://stackoverflow.com/a/66830126/7896203 it fixed this for me – Reed Aug 19 '21 at 15:18
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • This was my exact problem. Shutting down VS Code, removing ALL github login credentials, and restarting VS Code fixed the problem for me. I just had to re-login to turn on Settings Sync and VS Code handled all the rest – S.Chauhan Sep 16 '21 at 18:07
3

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
snowbound
  • 1,692
  • 2
  • 21
  • 29
3

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://asdlkuresdflj23433423lj234@github.com/brother/sampleapp.git
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nadir
  • 63
  • 8
2

The following helped me (I am user Ubuntu 22.04): git config --local -e Edit your data ==>:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://YourGitlabName:YourToken@gitlab.com/YorRepo
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
[user]
        name = YourGitlabName
        email = YourGitlabE-mail
A.Gansior
  • 21
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33447252) – user12256545 Dec 21 '22 at 19:47
  • Works perfectly! Just replace **Gitlab** info with your **GitHub** info. – Arnav Jun 24 '23 at 07:17
0

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'.

Jesse
  • 439
  • 4
  • 6
  • 1
    Please explain "and finally got the 'Signing into GitHub' prompt to appear". Obviously you had difficulty with this and should eloborate. – Justin Solms Aug 20 '21 at 10:57
  • My issue was identical to that of the OP. – Jesse Sep 28 '21 at 20:19
  • 1
    *How* did you get the 'Signing into GitHub' prompt to appear? Is that a conclusion of this answer or something you achieved first? If the latter, *how*? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/68823502/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Nov 16 '21 at 21:33
  • @PeterMortensen I just came across this answer today. When did best practice change from being honest about adding "Update:" notes to your answer so as not to be deceptive and to have good continuity with your changes resulting from comment suggestions to what you said? – Eric Hepperle - CodeSlayer2010 Feb 26 '22 at 17:42
  • @Eric Hepperle - CodeSlayer2010: There isn't any deception when there is [a public revision history](https://stackoverflow.com/posts/66233446/revisions). See for example [this MSO question](https://meta.stackexchange.com/a/230693) and [this MSO question](https://meta.stackexchange.com/a/127655). – Peter Mortensen Mar 03 '22 at 09:10
0

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.

Terry Snow
  • 45
  • 1
  • 4