GitHub - 错误:无法读取“https://github.com”的用户名:没有这样的文件或目录

239

当我尝试在 Windows 上使用 Git Bash 拉取代码时,出现了以下问题:

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

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

然而问题仍然存在。

在添加/删除源之后,我仍然遇到相同的错误。

能否就此问题提供建议?

谢谢!


2
你配置了用户名吗? git config --global user.name "Your Name Here" - Raul Andres
我已经尝试过了,但它没有引用我的 git 用户名,我认为它是因为我在这里调用的 git 存储库的用户名出现问题了。 - noobie-php
1
简单地重新启动解决了我的问题。 - m_ocean
不要被“找不到文件或目录”所分散注意力,最好的做法是认为错误提示只是表明你需要向命令添加身份验证(因为它是私有存储库等)。例如,通过使用私有访问令牌。 - klaus thorn
33个回答

132

请按照以下步骤设置SSH密钥:https://help.github.com/articles/generating-ssh-keys

或者

git remote add origin https://{username}:{password}@github.com/{username}/project.git

更新:如果你遇到"fatal: remote origin already exists."错误,则需要使用set-url命令:
git remote set-url origin https://{username}:{password}@github.com/{username}/project.git

3
第二个选项不起作用,因为我已经添加了“origin github”。我删除了 origin(git remote remove origin),然后按照答案中的用户名和密码进行操作,这样就可以正常运行了。 - P_Rein
16
你可以使用 git remote set-url origin https://{username}:{password}@github.com/... 命令来更改 URL,而不是删除并重新添加源。 - Chris
15
@Chris的解决方案有效,但它会存储用户的GitHub账户密码,并允许通过键入git remote -v来读取密码。 - Jefferson Silva
2
2021年8月13日已移除对密码验证的支持。 - Krzysztof Krzeszewski
2
很好。现在我们需要使用个人访问令牌而不是密码。 - Billal Begueradj
显示剩余4条评论

50

我遇到了完全相同的问题。当我使用 HTTPS URL 克隆一个仓库,然后尝试推送更改时,出现了这个问题(在 Linux/Mac 上使用 Shell 或者在 Windows 上使用 Git Bash):

git clone https://github.com/{username}/{repo}.git

然而,当我使用 SSH URL 进行克隆时,这个问题并没有出现:

git clone git@github.com:{username}/{repo}.git

如果您已经使用 HTTPS 克隆了代码库,而且不想重新克隆,您可以使用 set-url 将远程仓库的 URL 改为 SSH URL:

git remote set-url origin git@github.com:{username}/{repo}.git

注意: 我已经在我的GitHub帐户上添加了SSH密钥。 如果没有设置SSH密钥,则此方法也将不起作用。

顺便说一句,git clonepull应该可以正常工作,无需进行任何更改。只有当您在clone之后使用git push时才需要这些更改。


16
好的,但是为什么会发生这种情况呢? - Gili
9
在运行于Gitlab的Docker容器中,我收到了这条消息。基础镜像是python:3.7-slim,因此与Windows无关。 - Martin Thoma
在我的情况下,这是一个使用ssh://git.gartner.com的组织存储库。 - Ankush
4
只有当GitLab仓库设置为私有时,我在Docker容器上才会遇到这个错误。 - el_tenedor
1
GitLens 13.2.0 在 VSCode 中出现了“获取…”消息,直到按照此处所述切换为 ssh,才能消除无错误消息的情况。 - dlauzon
显示剩余4条评论

44

我在这里找到了答案:

编辑~/.gitconfig,并添加以下内容:

[url "git@github.com:"]
 insteadOf = https://github.com/

虽然它解决的问题不同,但错误代码是相同的...


1
现在您需要一个SSH密钥来使其工作,例如在纯CI上,它只会通过https进行git克隆而不需要SSH密钥,对吧? - Ivan Borshchov
我的回答只适用于那些尝试使用ssh克隆并卡在https方法上的人。要使用ssh,您需要git@...而不是https://...网址。 - Louis Parkin

25

请查看以下内容

Android Studio -> 首选项 -> 版本控制 -> Git -> 使用凭据助手


4
什么情况下推荐使用这种方法?此外,你能解释一下凭证助手是干什么的吗? - aaossa
它考虑了系统凭据。对我有用。非常感谢! - cV2
这也是我的问题,更新后的路径是设置 -> 版本控制 -> Git -> 使用凭据助手。 - Akash Jain
Windows:文件 > 设置 > Git > 使用凭据助手 - Haider Malik
所以,这对我起作用了,我很想知道为什么。 - pjaaar

15

对于我来说,以上建议都没有起作用,我在Jenkins Shell脚本中使用git pull命令,但显然它使用了错误的用户名。在我找到一种不需要切换到SSH的方法之前,我花费了很长时间。

在您的用户文件夹中创建.gitconfig文件(如果您还没有),并以以下格式输入您的凭据:https://user:pass@example.com,更多信息。在您的.gitconfig文件中链接到这些凭据,对我来说是这样:

[credential]
   helper = store --file /Users/admin/.git-credentials

现在git将始终使用这些凭据,无论如何。我希望它能帮助某人,就像它帮助了我一样。


只有当我在我的工作目录中将[credential] helper = store --file /..../.git-credentials放入.git/config时,它才对我起作用。 - Kirill Oficerov

13
如果您想继续使用https而不是ssh,并且出于安全原因避免输入用户名和密码,您还可以尝试Github OAuth token,然后您可以执行以下操作:
git config remote.origin.url 'https://{token}@github.com/{username}/{project}.git'
或者
git remote add origin 'https://{token}@github.com/{username}/{project}.git'
这对我很有效!

12
注意,如果您遇到以下错误:
fatal: could not read Username for 'https://github.com': No error

那么您需要将Git更新到版本2.16或更高版本。


8

当尝试克隆无效的HTTP URL时,也会出现此错误。例如,当我尝试克隆一个GitHub URL时,由于URL有几个字符错误,因此出现了以下错误:

$ git clone -v http://github.com/username/repo-name.git
Cloning into 'repo-name'...
Username for 'https://github.com': 
Password for 'https://github.com': 
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/username/repo-name.git/'

实际上这是发生在 Emacs 中的,因此 Emacs 报错信息如下:

fatal: could not read Username for ’https://github.com’: No such device or address

所以,与其提示不存在该URL的repo,它直接让我这样干,一直找寻下去,最后才意识到URL不正确。

这是使用git 2.7.4时出现的情况。

我在此发布此信息,因为一个月前它发生过,并且刚刚再次出现,导致我再次陷入了同样的困境。 >:(


8

TL;DR:检查是否可以读写/dev/tty。如果不能,并且您已使用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开始正常工作了。


5

尝试了这里的所有方法,但仍然无法解决问题。

不过,当我尝试通过git config --system --list进行调试时,出现了fatal: unable to read config file '/etc/gitconfig': No such file or directory

因此,我进行了以下操作,

sudo ln -s $HOME/.gitconfig /etc/gitconfig

然后,瞧,它就能工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接