ssh:无法解析主机名 git:名称或服务未知 致命:无法从远程存储库读取

Maj*_*abi 17 git gitlab

在我为我的 repo 从 HTTPS 切换到 SSH 之后,我在推送到 origin master 时收到了这个错误:

ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

我还在 gitlab 中添加了我的 ssh。我该怎么办?

Shi*_*rma 16

我遇到了同样的错误。

更改或文件中的[remote "origin"] url.gitconfig.config/git/config

之前:

https://git@github.com:userName/repo.git

或者

ssh://git@github.com:userName/repo.git

新的:

git@github.com:userName/repo.git


Dhr*_*shi 11

HTTPS URL 提供对私有或公共存储库的轻松访问,即使您在防火墙或代理后面工作。这是推荐的方式。

另一方面,SSH URL 使用安全的 ssh 协议来访问存储库。

回到你的问题,错误很可能是因为配置不当。该git remote add命令用于向您的存储库添加一个您已经尝试过的新遥控器。但是,从 HTTPS 切换到 SSH url,意味着您的远程源已经设置为 http url 并且您想要更改。

因此,首先检查您当前的远程源所指的网址:

$ git remote -v
Run Code Online (Sandbox Code Playgroud)

如果它指的是 HTTPS 网址,那么您必须使用

$ git remote set-url origin mySSH_url
Run Code Online (Sandbox Code Playgroud)

命令将其更改为 SSH url。

现在,尝试git remote -v,它将显示为 origin 配置的 SSH url。

请确保在使用 SSH url 时,您已经ssh-agent在 GitLab/GitHub 帐户上生成并添加了 ssh 密钥。

是一篇关于如何更改遥控器网址的非常好的文章。

此外,您可以在此处了解有关使用哪个远程 url 的更多信息。


小智 6

我在使用 azure 时遇到了同样的问题。

ssh: Could not resolve hostname ssh.abc.azure.com: Temporary failure in name 
resolution
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository 
exists.
Run Code Online (Sandbox Code Playgroud)

原因:该问题是由于WSL2失败造成的。

修复: 禁用并启用 WSL。

  • 谢谢。就我而言,这是 WSL 问题。通常每 2 个月就会出现此错误。我通常尝试重新配置 VPN,重新启动系统。这次,我尝试重新启动 WSL 服务,它开始工作。 (2认同)

Rah*_*edi 5

好吧,就我而言,我的本地系统未连接到 VPN,因此出现此错误。因此,这可能是上述答案之外的原因之一。