【git 端口拒绝解决方案】ssh: connect to host github.com port 22: Connection refused

前言

前一阵儿,在公司同步github代码到本地的时候,爆出了这样的一个错误ssh: connect to host github.com port 22: Connection refused。根据英文可以看出,ssh端口号被拒绝了,应该是被公司的网给禁掉了。

总结下如何换一种方式解决。

git 远程仓库两种协议

在解决问题之前,先要了解git远程仓库的两种协议连接

ssh协议连接github

在windows下的git客户端
这里写图片描述

在用户的.ssh下生成了两个SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

这里写图片描述

$ ssh-keygen -t rsa -C "youremail@example.com"

这里写图片描述

这里写图片描述

打开本地的id_rsa.pub(公钥),将内容复制进去即可

这里写图片描述

github端配置完毕后,看本地的git 如何添加远程仓库,以下是重头戏:

第一步,查看当前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
  • 41
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
这个错误通常是由于连接到GitHubSSH端口端口22)被拒绝或超时引起的。有几种可能的解决方法: 1. 参考中的解决方案,可以尝试在SSH配置文件中更改GitHub的主机名为ssh.github.com,并将端口设置为443。这样可以使用git pull命令来拉取代码。 2. 参考中的解决方法,可以尝试使用HTTP连接而不是SSH连接。通过修改.git/config文件中的URL,将其改为使用https://github.com/username/repo.git。 3. 参考中的解决方案,可以尝试在SSH配置文件中添加一些内容,包括将GitHub的主机名设置为ssh.github.com,并将端口设置为443。 请注意,端口22被封锁或超时可能是因为网络配置或防火墙设置。如果上述解决方法均无效,建议与网络管理员或GitHub支持团队联系,以获得更多帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ssh: connect to host github.com port 22: Connection refused fatal: Could not read from remote reposi...](https://blog.csdn.net/weixin_41697143/article/details/129012461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [解决git报错:ssh:connect to host github.com port 22: Connection timed out](https://blog.csdn.net/yjxkq99/article/details/128927038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值