Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 854
user1186050 Avatar asked Feb 16 '21 19:02

user1186050


People also ask

How use GitHub personal access token in VS Code?

Setup Personal Access Token The 'GitHub: Set Personal Access Token' should be executed for that. To execute the 'GitHub: Set Personal Access Token' type Ctrl+Shift+p in VSCode to open the command palette and type 'GitHub: Set Personal Access Token'. You will then be prompted to enter the token generated from GitHub.

How do I add a personal access token to Visual Studio Code?

If you are not redirected to VS Code, you can add your authorization token manually. In the browser window, you will receive your authorization token. Copy the token, and switch back to VS Code. Select Signing in to github.com... in the Status bar, paste the token, and hit Enter.

How do I add GitHub link to Visual Studio?

In Visual Studio, select Team Explorer from the View menu. In the Team Explorer pane, click the Manage Connections toolbar icon. Click the Connect link in the GitHub section. If none of these options are visible, click Manage Connections and then Connect to GitHub.


Video Answer


5 Answers

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
    
like image 51
Giddy Naya Avatar answered Oct 19 '22 06:10

Giddy Naya


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)
like image 31
sultanmyrza Avatar answered Oct 19 '22 06:10

sultanmyrza


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

like image 13
KyleMit Avatar answered Oct 19 '22 04:10

KyleMit


  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.

like image 2
Chris Howard Avatar answered Oct 19 '22 04:10

Chris Howard


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

like image 2
Mohammed Taher Avatar answered Oct 19 '22 04:10

Mohammed Taher