我开始使用Visual Studio Code,我试图将我的测试项目保存到GitHub,但Visual Studio Code总是要求我的GitHub凭据。

我已经在我的PC上安装了GitHub Desktop和Git。我已经跑了:

 git config --global credential.helper wincred

但是Visual Studio Code仍然在要求凭证。

我该如何解决这个问题?

下面是我的.gitconfig文件,位于用户配置文件文件夹中:

    [filter "lfs"]
    clean = git-lfs clean %f
    smudge = git-lfs smudge %f
    required = true
[user]
    name = ddieppa
[user]
    email = fake@live.com
[credential]
    helper = wincred

下面是弹出窗口要求凭据:

我在弹出窗口中输入了我的GitHub凭证,但仍然在Visual Studio Code中的Git输出窗口中得到这个错误:

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

当前回答

Windows 10:

按窗口键。 输入信用,你应该看到“凭证管理器”在控制面板 单击此处打开并删除相关的缓存凭据。然后再试一次。它将询问用户ID和密码。输入正确的密码就可以了。

当我修改我的网络密码时,就发生了这种情况。

其他回答

对于我来说,我已经用SSH密钥设置了远程存储库,但是Git无法找到它们,因为HOMEDRIVE环境变量由于公司的域策略被自动设置为网络共享。在启动代码之前更改shell中的环境变量。导致Visual Studio Code继承正确的环境变量和voilà在Git输出窗口中不再有连接错误。

现在我只需要弄清楚如何覆盖域策略,因此HOMEDRIVE总是指向我的本地c:\users\marvhen目录,这是.ssh目录的默认位置。

使用SSH代替HTTP或HTTPS。

您需要在您的本地机器上设置SSH密钥,将它们上传到您的Git服务器,并将URL从http://替换为Git://,您将不再需要使用密码。

如果你不能使用ssh添加到你的配置:

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

文件在这里。


在GitHub中使用SSH密钥


简单地按照这些步骤,你将设置你的SSH密钥在任何时间:

生成一个新的SSH密钥(如果已经有密钥,则跳过此步骤) ssh -t rsa -C "your@email" 一旦你在家里设置好了钥匙。ssh目录(或Users/<您的用户>。ssh),打开并复制内容


如何向GitHub帐户添加SSH密钥?

登录GitHub账户 点击右上角的牧场主(设置) GitHub账户设置http://git-scm.com/docs/gitcredentials 单击SSH密钥 单击Add ssh键 粘贴密钥并保存

你都准备好了:-)

为什么Visual Studio Code需要密码?因为Visual Studio Code运行自动获取功能,而Git服务器没有任何信息来授权您的身份。它发生在:

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.

这个答案后面更新的部分对你一点帮助都没有。(这实际上会让你的工作流程停滞不前。)它只是停止在Visual Studio Code中发生的事情,并将这些发生的事情移动到终端。

如果这个糟糕的答案影响了你很长很长一段时间,我很抱歉。


原来的答案(不好)

我在Visual Studio Code文档中找到了解决方案:

提示:您应该设置一个凭据助手,以避免被询问 每次VS Code与你的Git遥控器对话时,你都需要获取凭据。如果你 不要这样做,你可能想要考虑在… 菜单来减少提示的数量。

因此,打开凭据帮助程序,以便Git将密码保存在内存中一段时间。默认情况下,Git会缓存密码15分钟。

在Terminal中输入如下内容:

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

输入如下命令修改默认密码缓存超时时间。

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

如果原来的答案不成立

我安装了Visual Studio Code和上面相同的配置,但正如@ddieppa所说,它也不适合我。所以我试着在用户设置中找到一个选项,我看到了“git”。Autofetch " =真,现在设置为假!Visual Studio代码不再需要重复输入密码!

在菜单文件→首选项→用户设置: 类型:

将您的设置放在这个文件中以覆盖默认设置:

{
  "git.autofetch": false
}

在与这样的事情斗争了一段时间后,我想我想出了一个很好的解决方案,尤其是在GitHub和Bitbucket上有多个账户的时候。然而,对于Visual Studio Code,它最终从Git Bash终端启动,这样它从Bash会话继承了环境变量,并且它知道要查看哪个ssh-agent。

我仍然很难找到一个地方来获取我需要的信息。另外,自2017年以来,ssh-agent只有在您尝试访问存储库时才会提示您输入密码短语。

我把我的发现写在这里,如果有人感兴趣的话。

我通常运行这个简单的命令来将Git远程URL从HTTPS更改为SSH:

git remote set-url origin git@github.com:username/repo-name-here.git