为什么我在运行 Win10 Linux 子系统时,Github 上总是出现“身份验证失败”?

soa*_*gem 1 linux git github two-factor-authentication windows-subsystem-for-linux

我的计算机运行 Windows 10,启用了开发人员模式并安装了 Linux 子系统。当我切换到 bash 时,我尝试从 git 运行一个简单的克隆命令:

git clone https://github.com/PrivateRepo/my-private-repo.git
Run Code Online (Sandbox Code Playgroud)

当然,我已经稍微删除了此处的 URL,但您已经明白了。最初,我从该命令得到的响应是这样的:

致命:无法访问“ https://github.com/PrivateRepo/my-private-repo.git/ ”:服务器证书验证失败。CA 文件:/etc/ssl/certs/ca-certificates.crt CRL 文件:无

我看到一些 Stack Overflow 答案谈到了这个问题,但说实话,我真的不知道处理该错误的正确方法。话虽如此,我知道一种不正确的方法,那就是通过运行以下命令来完全规避它:

git config --global http.sslverify false
Run Code Online (Sandbox Code Playgroud)

这似乎有效。我关闭所有 SSL 验证可能很糟糕,但我需要让它正常工作,而且这并不是这个问题的重点(我正在讨论)。但顺便说一句,如果你知道解决这个问题的正确方法,我会洗耳恭听。

接下来,我重新运行git clone命令,这一次它提示我输入用户名和密码,但最终由于不同的原因而失败。它看起来是这样的:

git clone https://github.com/PrivateRepo/my-private-repo.git
Cloning into 'my-private-repo'...
Username for 'https://github.com': myuser
Password for 'https://myuser@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/PrivateRepo/my-private-repo.git'
Run Code Online (Sandbox Code Playgroud)

不过,我可以向您保证,我输入的用户名和密码都是正确的。我还安装了 Windows 版 Git,当我尝试时一切正常。唯一需要注意的是,它没有在命令行中提示我输入密码,而是打开了一个单独的窗口来输入凭据,并且还要求我提供六位数的 2FA 代码。然后它克隆得很好。我注意到这样做会在 Github 中创建一个个人访问令牌。因此,我继续创建了另一个个人访问令牌,并尝试在 bash 中手动使用它。基本上不是输入myuser我输入的用户名myuser:mytoken

git clone https://github.com/PrivateRepo/my-private-repo.git
Cloning into 'my-private-repo'...
Username for 'https://github.com': myuser:a1abcdefab2a34567ab8901a2bc3d4567890a1b2
Password for 'https://myuser:a1abcdefab2a34567ab8901a2bc3d4567890a1b2@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/PrivateRepo/my-private-repo.git'
Run Code Online (Sandbox Code Playgroud)

但正如你所看到的,它仍然失败了。如何让 git 在 Windows 上的 bash 中工作?

小智 5

您正在当前运行的 Linux 发行版上使用 的实现git,您可能没有设置它。尝试使用git.exe

git.exe clone https://github.com/PrivateRepo/my-private-repo.git
Run Code Online (Sandbox Code Playgroud)

您可以alias在您的.bashrc

alias git='git.exe'
Run Code Online (Sandbox Code Playgroud)