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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53129706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:21:02  来源:igfitidea点击:

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

gitgitlab

提问by Majid Rajabi

After I switched from HTTPS to SSH for my repo then I received this error when pushing to origin master:

在我为我的 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.

I also add my ssh in the gitlab. What should I do?

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

采纳答案by Shivam Sharma

I was getting the same error.

我遇到了同样的错误。

I changed the [remote "origin"]urlvalue in .git/configfile and now it's working fine.

我更改了文件中的[remote "origin"]url.git/config,现在工作正常。

Previously:

之前:

https://[email protected]:userName/repo.git

https://[email protected]:userName/repo.git

OR

或者

ssh://[email protected]:userName/repo.git

ssh://[email protected]:userName/repo.git

New:

新的:

[email protected]:userName/repo.git

[email protected]:userName/repo.git

回答by Dhruv Joshi

HTTPS URLs provides an easy access to your repository, either private or public, even when you are working behind a firewall or proxy. And it is the recommended way.

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

On the other hand, SSH URLs use the secure ssh protocol in order to access a repository.

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

Coming back to your question, the error is likely because of improper configuration. The git remote addcommand is used to add a new remote to your repository, that you have already tried. However, switching from HTTPS to SSH url, means that your remote origin is already set to an http url and that you want to change.

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

Therefore, first check what url your current remote origin is referring to:

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

$ git remote -v

If it is referring to the HTTPS url, then you have to use

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

$ git remote set-url origin mySSH_url

command to change it to the SSH url.

命令将其更改为 SSH url。

Now, try git remote -v, it would display SSH urls configured for origin.

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

Do make sure that while working with SSH urls, you have generated and added the ssh key to the ssh-agentas well on GitLab/GitHub account.

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

Hereis a very good article on how to change a remote's url.

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

Also, you can learn more about which remote url to use here.

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

回答by Rahul Dwivedi

Well, in my case my local system was not connected to the VPN and hence was getting this error. So this could be one of the reasons apart from the above answers.

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