ddieppa
ddieppa

Reputation: 6356

Visual Studio Code is always asking for Git credentials

I started using Visual Studio Code, and I was trying to save my test project into GitHub, but Visual Studio Code is always asking for my GitHub credentials.

I have installed in my PC GitHub Desktop and also Git. I already ran:

 git config --global credential.helper wincred

but still Visual Studio Code is asking for the credentials.

How can I fix this?

Here is my .gitconfig file located in the user profile folfer:

    [filter "lfs"]
    clean = git-lfs clean %f
    smudge = git-lfs smudge %f
    required = true
[user]
    name = ddieppa
[user]
    email = [email protected]
[credential]
    helper = wincred

Here is the popup windows asking for the credentials:

Enter image description here

I enter my GitHub credentials in the popup, but still getting this error in the Git output window in Visual Studio Code:

remote: Anonymous access to ddieppa/LineOfBizApp.git denied.
fatal: Authentication failed for 'https://github.com/ddieppa/LineOfBizApp.git/'

Upvotes: 396

Views: 642576

Answers (30)

Sau001
Sau001

Reputation: 1664

I had a similar issue. I re-authenticated my Github Desktop as follows (on Windows 10):

Step-1

  1. Launch Github Desktop
  2. Click on Options menu item

Options menu item

Step-2

  1. Make the desktop to re-authenticate by first signing out
  2. The button caption now changed to "Sign in". sign in button

Step-3

In the above UI - I clicked on the "Sign in" button and this launched the browser which carried out the usual 2-factor authentication.

With the above 3 steps done - my VS Code was once again able to interact with git via the VS Code interface.

Hope this helps.

Upvotes: 0

Júlio Cesar
Júlio Cesar

Reputation: 1

Create a token in Settings > Developer Settings > Personal access tokens > Tokens (Classic) Then use the url like this in your git config file [remote "origin"] url = https://[email protected]/your/repository.git

Upvotes: 0

Sargis Manasyan
Sargis Manasyan

Reputation: 1

For me, the problem was:

I was connected to GitHub and now I wanted to switch to Azure DevOps and VS Code was asking me for authentification for any action.

Solution is:

Open Credential Manager -> Windows Credentials

Remove credential that starts with "git:"

Open the project in VS Code and click Synchronize changes. Log in with your credentials.

Upvotes: 0

Amirhosein Aghasalehi
Amirhosein Aghasalehi

Reputation: 31

this may possibly happen when you are using an "https" address of git repository like https://github.com:TEST/PROJECT.git. you can fix this issue by replacing the https address with ssh address, by making a change on this file: PROJECT ROOT/.git/config you should change url and pushurl to the git one address.

Upvotes: 0

Vaibhav Vishal
Vaibhav Vishal

Reputation: 7108

This has been working for me:

  1. Set the credential helper to store:

    git config --global credential.helper store

  2. Then verify if you want:

    git config --global credential.helper

A simple example when using Git Bash quoted from here (works for the current repository only, use --global for all repositories):

git config credential.helper store
git push http://example.com/repo.git

Username: < type your username >
Password: < type your password >

[several days later]

git push http://example.com/repo.git

[your credentials are used automatically]

It will work for Visual Studio Code too.

A more detailed example and advanced usage is here.

Note: Username and passwords are not encrypted and are stored in plain text format, so use it on your personal computer only.

Upvotes: 198

GoTo
GoTo

Reputation: 6666

All I had to do was to run this command:

Windows: git config --global credential.helper wincred

Linux: git config --global credential.helper store

Then I was prompted for the password twice.

Next time it worked without prompting me for a password.

Upvotes: 55

Maciej Skorski
Maciej Skorski

Reputation: 3344

We need to configure VS Code to provide authentication on top of Git commands.

Below I show how to set up the extension settings.

enter image description here

Upvotes: 0

NikkiQin
NikkiQin

Reputation: 19

This works for me. you only need to type in PAT once. git config --global credential.helper wincred

Upvotes: 0

Lucas Basquerotto
Lucas Basquerotto

Reputation: 8031

(This answer is not specific to GitHub, instead I was using BitBucket, but it may work in GitHub too, because the concept is the same)

In my case I was using a Bitbucket app password and it always asked me to authenticate.

Stopping automatic fetch doesn't solve the problem because I pull and push several times in a day, and entering the app password everytime is just bad.

Furtermore, I would need to store the app password somewhere unsafe, which is also the reason that I don't want to use credential.helper store, that stores the password in plaintext, and instead use credential.helper wincred, which is encrypted.

When the dialog prompted me, instead of using the app password, I asked to sign-in using the browser, which worked, for reasons that are beyond my understanding. Now it doesn't ask for credentials anymore (I had to change my default browser temporarily, because the bitbucket account I use for work is not in my default browser, and the dialog try to authenticate using the default browser).

Upvotes: 0

Davuz
Davuz

Reputation: 5266

Why does Visual Studio Code ask for a password? Because Visual Studio Code runs the auto-fetch feature, while the Git server doesn't have any information to authorize your identity. It happens when:

  • Your Git repository has a https remote URL. Yes! This kind of remote will absolutely ask you every time. No exceptions here! (You can do a temporary trick to cache the authorization as the solution below, but this is not recommended.)
  • Your Git repository has ssh remote URL, but you've not copied your ssh public key onto the Git server. Use ssh-keygen to generate your key and copy it to Git server. Done! This solution also helps you never retype password on terminal again. See a good instruction by @Fnatical here for the answer.

The updated part at the end of this answer doesn't really help you at all. (It actually makes you stagnant in your workflow.) It only stops things happening in Visual Studio Code and moves these happenings to the terminal.

Sorry if this bad answer has affected you for a long, long time.


The original answer (bad)

I found the solution on Visual Studio Code document:

Tip: You should set up a credential helper to avoid getting asked for credentials every time VS Code talks to your Git remotes. If you don't do this, you may want to consider Disabling Autofetch in the ... menu to reduce the number of prompts you get.

So, turn on the credential helper so that Git will save your password in memory for some time. By default, Git will cache your password for 15 minutes.

In Terminal, enter the following:

git config --global credential.helper cache
# Set Git to use the credential memory cache

To change the default password cache timeout, enter the following:

git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)

If the original answer doesn't work

I installed Visual Studio Code and configuration same above, but as @ddieppa said, It didn't work for me too. So I tried to find an option in User Setting, and I saw "git.autofetch" = true, now set it's false! Visual Studio Code is no longer required to enter password repeatedly again!

In menu FilePreferencesUser Setting: Type these:

Place your settings in this file to overwrite the default settings:

{
  "git.autofetch": false
}

Upvotes: 242

Dhruv Rangunwala
Dhruv Rangunwala

Reputation: 226

This is how I solved the issue on my computer:

  1. Open Visual Studio Code

  2. Go to menu FilePreferencesSettings

  3. Under User tab, expand Extensions and select Git

    Enter image description here

  4. Find Autofetch on the right pane and uncheck it

    Enter image description here

Upvotes: 1

Rohit Paliwal
Rohit Paliwal

Reputation: 11

For Windows 10:

  • Press the windows key.
  • Type cred and you should see "Credential Manager" in Control Panel
  • Click to open and then remove the related cached credentials. Then try again. It will ask for a user ID and a password. Key in the correct password and you'll be good.

It happened with me when I changed my network password.

Upvotes: 1

Adam Erickson
Adam Erickson

Reputation: 6363

In general, you can use the built-in credential storage facilities:

git config --global credential.helper store

Or, if you're on Windows, you can use their credential system:

git config --global credential.helper wincred

Or, if you're on macOS, you can use their credential system:

git config --global credential.helper osxkeychain

The first solution is optimal in most situations.

Upvotes: 39

Vland
Vland

Reputation: 4262

I had a similar problem in Visual Studio Code.

I solved it by changing the remote URL to https (in file .git/config):

[remote "origin"]
    url = https://[email protected]/plesk-git/project.git

And also:

git config --global credential.helper wincred

I pulled again, the Windows credentials popup came out, and the problems were solved.

Upvotes: 2

Sundar
Sundar

Reputation: 665

Solve the issue by the following steps.

Uninstall software Visual Studio Code and Git and reinstall the same. Change the Git repository clone URL from SSH to HTTPS.

Upvotes: 1

ubreddy
ubreddy

Reputation: 875

Apart from adding the SSH configuration entries above

If Windows

Set-Service ssh-agent -StartupType Automatic

In PowerShell.

Now Visual Studio Code should not prompt...

Upvotes: -1

Meera
Meera

Reputation: 71

For Windows 10: go to Control PanelCredential managerWindows Credential → click on the Git link, → Edit → update to new password. That should work.

Upvotes: 7

Avinash
Avinash

Reputation: 902

I solved a similar problem in a simple way:

  1. Go to CMD or Terminal
  2. Type git pull origin master. Replace 'origin' with your remote name
  3. It will ask for the credentials. Type it.

That's all. I solved the problem.

Upvotes: 0

Mick
Mick

Reputation: 11

I managed to stop this by carrying out the following steps.

  1. Uninstall GitHub Desktop (I am not sure this is necessary)
  2. Uninstall Credentials Manager: cd "C:\Program Files\Git\mingw64\libexec\git-core" followed by git-credential-manager.exe uninstall
  3. Reset the credentials helper to use wincred: git config --global credential.helper wincred
  4. Use Visual Studio Code to push some changes and reinput credentials.

NOTE: I was using a personal access token as my password.

Upvotes: 1

marvhen
marvhen

Reputation: 341

For me, I had set up my remote repository with an SSH key, but Git could not find them because the HOMEDRIVE environment variable was automatically getting set to a network share due to my company's domain policy. Changing that environment variable in my shell prior to launching code . caused Visual Studio Code to inherit the correct environment variable and voilà no more connection errors in the Git output window.

Now I just have to figure out how to override the domain policy, so HOMEDRIVE is always pointing to my local c:\users\marvhen directory, which is the default location for the .ssh directory.

Upvotes: 0

Fnatical
Fnatical

Reputation: 576

The following steps walk you through how to:

  1. Generate SSH keys (without passphrase**)
  2. Add the public key generated in step 1 to your Git repository
  3. Confirm the above steps have been completed successfully
  4. Make your first commit (without having to provide a username / password)

**Generating SSH keys without a passphrase is unwise if your work is particularly sensitive.

OS - Fedora 28 | Editor - Visual Studio Code v1.23.0 | Repository - Git

Generate SSH keys:

  • ssh-keygen -t rsa -C "[email protected]"
  • Enter file in which to save the key: Press Enter
  • Enter passphrase: Press Enter
  • Enter same passphrase again: Press Enter

After completing the above steps, the location of your public key is shown in the terminal window. If the currently logged in user is 'bob' the location of your public key would be /home/bob/.ssh/id_rsa.pub

Copy and import public key to GitHub:

  • cat /home/bob/.ssh/id_rsa.pub

  • Copy the whole public key that is now displayed in your terminal window to the clipboard

  • Go to https://github.com and sign in

  • Click the user icon in the top right corner of the screen and select Settings

  • Click SSH and GPG keys

  • Click New SSH key

  • Enter a title, paste the public key copied to the clipboard in the first bullet point, and click Add SSH key

Confirm the above steps:

  • ssh -T [email protected]

  • yes

  • Hi ! You've successfully authenticated, but GitHub does not provide shell access.

First commit / push without having to enter a username / password:

  • touch test.txt

  • git add test.txt

  • git commit - opens editor, enter a message and save the file. If vi is your editor, press I once the file opens, enter a message, press Esc, and then enter :x to save changes.

  • git push

The only hiccup you may encounter is when you attempt to SSH to GitHub. This link will provide some assistance -

Happy hunting!

Upvotes: 40

yohosuff
yohosuff

Reputation: 1459

You should be able to set your credentials like this:

git remote set-url origin https://<USERNAME>:<PASSWORD>@bitbucket.org/path/to/repo.git

You can get the remote URL like this:

git config --get remote.origin.url

Upvotes: 121

Paul Paku
Paul Paku

Reputation: 361

Following this article:

You may just set the GIT_SSH environment variable to PuTTY's plink.exe program.

(Then use the pageant.exe program as an authentication agent.)

Upvotes: 1

Reddog
Reddog

Reputation: 15579

Try installing "Git Credential Manager For Windows" (and following instructions for setting up the credential manager).

When required within an application using Git (e.g., Visual Studio Code) it will "magically" open the required dialog for Visual Studio Team Services credential input.

Upvotes: 24

Ahmad Khudeish
Ahmad Khudeish

Reputation: 1137

I usually run this simple command to change the Git remote URL from HTTPS to SSH:

git remote set-url origin [email protected]:username/repo-name-here.git

Upvotes: 3

Tejas Savaliya
Tejas Savaliya

Reputation: 638

You can refer to this link to set up a Git credential.

You can run the following command to save your Git credentials. You do not need to enter a username and password every time the Git command runs (it’s for Windows):

git config --global credential.helper wincred

For Mac / Linux, see Git Credential Manager for Mac and Linux for how to save Git credentials.

Upvotes: 0

ScubaManDan
ScubaManDan

Reputation: 839

After fighting with something like this for a little while, I think I came up with a good solution, especially when having multiple accounts across both GitHub and Bitbucket. However for Visual Studio Code, it ultimately ended up as start it from a Git Bash terminal so that it inherited the environment variables from the bash session and it knew which ssh-agent to look at.

I still really struggled to find one place to get the info I needed. Plus since 2017, ssh-agent got the ability to prompt you for a passphrase only when you try to access a repository.

I put my findings down here if anyone is interested.

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 141956

Use SSH instead of HTTP or HTTPS.

You will need to set SSH keys on your local machine, upload them to your Git server and replace the URL form http:// to git:// and you will not need to use passwords any more.

If you cant use ssh add this to your configuration:

[credential "https://example.com"]
    username = me

Documents are here.


Using an SSH key in GitHub


Simply follow these steps and you will set up your SSH key in no time:

  • Generate a new SSH key (or skip this step if you already have a key) ssh-keygen -t rsa -C "your@email"

  • Once you have your key set in home/.ssh directory (or Users/<your user>.ssh under Windows), open it and copy the content


How can I add an SSH key to a GitHub account?

And you are all set to go :-)

Upvotes: 22

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

In case you are on a Windows PC, then install Git for Windows. That's it!

Upvotes: -1

Himanshu Verma
Himanshu Verma

Reputation: 141

Ubuntu users should simply enter this command in the terminal of Visual Studio Code:

git config --global credential.helper store

Now, enter one time your username and password and after that, it won't ask you again.

Note:

  1. Username and passwords are not encrypted and stored in plain text format so use them with caution.

  2. But, if your password contains some special characters like @, #, &, * etc, then these characters will be encrypted.

Also, you can find your '.git-credentials' file at home. It's hidden, so make sure to enable hidden files.

Location: ~/.git-credentials

Upvotes: 6

Related Questions