当我试图在Windows上使用git Bash拉代码时,我有以下问题:

fatal: could not read Username for 'https://github.com': No such file or directory

我已经尝试实现这里提供的可接受的解决方案:

当推送提交Github时错误:致命:无法读取用户名

然而,问题仍然存在。

在添加/删除原点后,我仍然得到相同的错误。

你能给我一些建议吗?

谢谢!


当前回答

我通过清除无效缓存得到了答案,然后清洁构建帮助我。

选择“File > Invalidate Caches / Restart”,然后单击 “无效并重新启动”按钮。 选择Build > Clean Project,然后选择Build > Rebuild 项目。

其他回答

如果你想继续使用https代替ssh,出于安全考虑,不要输入你的用户名和密码。

你也可以试试Github OAuth令牌,然后你就可以了 Git配置remote.origin.url 'https://{token}@github.com/{username}/{project}.git' 或 Git远程添加原点“https://{token}@github.com/{username}/{project}.git”

这对我很管用!

TL;DR:检查是否可以读写/dev/ ty.如果没有,并且已经使用su打开shell,请检查是否使用正确。

我也遇到了同样的问题,但在Linux上,我发现了这个问题。我没有存储我的证书,所以我总是在提示时输入它们:

Username for 'https://github.com': foo
Password for 'https://foo@github.com': 

git处理http(s)连接的方式是使用/usr/lib/git-core/git-remote-https 你可以在这里看到strace:

stat("/usr/lib/git-core/git-remote-https", {st_mode=S_IFREG|0755, st_size=1366784, ...}) = 0
pipe([9, 10])                           = 0
rt_sigprocmask(SIG_SETMASK, ~[RTMIN RT_1], [], 8) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f65398bb350) = 18177
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
close(10)                               = 0
read(9, "", 8)                          = 0
close(9)                                = 0
close(5)                                = 0
close(8)                                = 0
dup(7)                                  = 5
fcntl(5, F_GETFL)                       = 0 (flags O_RDONLY)
write(6, "capabilities\n", 13)          = 13
fstat(5, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
read(5, "fetch\noption\npush\ncheck-connecti"..., 4096) = 38
write(6, "option progress true\n", 21)  = 21
read(5, "ok\n", 4096)                   = 3
write(6, "option verbosity 1\n", 19)    = 19
read(5, "ok\n", 4096)                   = 3
stat(".git/packed-refs", {st_mode=S_IFREG|0664, st_size=675, ...}) = 0
lstat(".git/objects/10/52401742a2e9a3e8bf068b115c3818180bf19e", {st_mode=S_IFREG|0444, st_size=179, ...}) = 0
lstat(".git/objects/4e/35fa16cf8f2676600f56e9ba78cf730adc706e", {st_mode=S_IFREG|0444, st_size=178, ...}) = 0
dup(7)                                  = 8
fcntl(8, F_GETFL)                       = 0 (flags O_RDONLY)
close(8)                                = 0
write(6, "list for-push\n", 14)         = 14
read(5, fatal: could not read Username for 'https://github.com': No such device or address
"", 4096)                       = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=18177, si_uid=1000, si_status=128, si_utime=6, si_stime=2} ---
exit_group(128)                         = ?
+++ exited with 128 +++

所以我试着直接调用它:

echo "list for-push" | strace /usr/lib/git-core/git-remote-https my

结果是:

poll([{fd=3, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 1 ([{fd=3, revents=POLLIN|POLLRDNORM}])
recvfrom(3, "\27\3\3\1\32", 5, 0, NULL, NULL) = 5
recvfrom(3, "\307|4Q\21\306\334\244o\237-\230\255\336\25\215D\257\227\274\r\330\314U\5\17\217T\274\262M\223"..., 282, 0, NULL, NULL) = 282
openat(AT_FDCWD, "/dev/tty", O_RDONLY)  = -1 ENXIO (No such device or address)
openat(AT_FDCWD, "/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 4
fstat(4, {st_mode=S_IFREG|0644, st_size=2995, ...}) = 0
read(4, "# Locale name alias data base.\n#"..., 4096) = 2995
read(4, "", 4096)                       = 0
close(4)                                = 0
openat(AT_FDCWD, "/usr/share/locale/en_US/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale-langpack/en_US/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale-langpack/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "fatal: could not read Username f"..., 83fatal: could not read Username for 'https://github.com': No such device or address
) = 83
exit_group(128)                         = ?
+++ exited with 128 +++

我突然想到:

openat(AT_FDCWD, "/dev/tty", O_RDONLY)  = -1 ENXIO (No such device or address)
...
write(2, "fatal: could not read Username f"..., 83fatal: could not read Username for 'https://github.com': No such device or address
) = 83

git-remote-https尝试通过/dev/tty读取凭据,所以我测试了它是否有效:

$ echo ahoj > /dev/tty
bash: /dev/tty: No such device or address

但在另一个终端:

# echo ahoj > /dev/tty
ahoj

我知道我用su切换到这个用户,所以我退出了shell,看看如何,发现我使用命令su danman -所以我再次测试了它:

~# su danman -
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
/root$ echo ahoj > /dev/tty
bash: /dev/tty: No such device or address

我可能忽略了这条信息,继续工作,但这就是原因。 当我切换使用正确的su - danman一切工作正常:

~# su - danman
danman@speedy:~$ echo ahoj > /dev/tty
ahoj

在此之后,git开始正常工作

下面是人们得到这个错误的简单原因:您试图从一个还没有设置本地git访问的回购中拉取。对我来说,当我将公共回购更改为私有回购时,我得到了这个错误。现在我很清楚,公共回购不需要认证,但私人回购需要。

希望这有助于澄清错误发生的原因以及该原因的潜在触发因素。

注意,如果你得到这个错误:

fatal: could not read Username for 'https://github.com': No error

然后,您需要将Git更新到2.16或更高版本。

这是系统凭据缓存中存储的凭据的问题。你可能有配置变量“credential”。Helper '设置为wincred或winstore,它无法清除它。如果启动控制面板并启动凭据管理器applet,那么在标记为git:https://github.com的通用凭据部分中寻找项目。如果您删除了这些,那么下次将重新创建,但是凭据帮助实用程序将要求您提供新的凭据。