ssh: connect to host github.com port 22: Connection refused 解决办法

这里承接上一篇文章,我自己新建项目后,竟然没有权限上传代码和pull,这里的解决办法很简单,就是把git地址换成http 的地址;

第一步,查看当前git的远程仓库版本:

$ git remote -v

此时若什么都没有显示说明,git无远程仓库。

第二步,添加ssh协议的远程仓库:

$ git remote add origin git@github.com:unlimitbladeworks/Data-Struts-Learning.git

再次查看

$ git remote -v
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (push)

当前,我本机就是用的这种方式连接的github,好处是每次提交代码时,不需要重复来回输入用户名和密码。

使用公司网合并前一天晚上更新到github上的代码时,报出如下错误:

$ git pull origin master
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

得知第一种协议被禁掉后,只能换一种连接进行合并本地仓库了。继续往下看另一种协议。

切换成 https协议连接github

依然是先查看当前远程仓库使用的那种协议连接:

$ git remote -v
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (push)

移除掉远程仓库的配置

$ git remote rm origin

重新添加新的远程仓库,以https的形式:

git remote add origin https://github.com/unlimitbladeworks/Data-Struts-Learning.git

再次查看:

$ git remote -v
origin  https://github.com/unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  https://github.com/unlimitbladeworks/Data-Struts-Learning.git (push)

https的地址其实就是github上项目对应的地址,如下图:


这里写图片描述

完事以上切换操作,其实问题就已经解决了。

最终结果

再次尝试pull代码,可以看到已经成功。

$ git pull origin master
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (8/8), done.
Unpacking objects: 100% (21/21), done.
remote: Total 21 (delta 5), reused 21 (delta 5), pack-reused 0
From https://github.com/unlimitbladeworks/Data-Struts-Learning
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容