Git问题:解决“ssh:connect to host github.com port 22: Connection timed out”

操作系统 Windows10
使用Git VScode
连接方式:SSH

在写完一天的代码后,我像往常一样点开了VScode的上传github按钮:
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200604225529465.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNzAzNg==,size_16,color_FFFFFF,t_70)
原本应该在几秒钟上传成功,但是却异常的慢,,这时突然出现一个错误弹窗(忘记截图了):

提示: “ssh:connect to host github.com port 22: Connection timed out”

再多尝试几次,依然是这样。

后来又尝试直接再文件夹里用git命令行提交:
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200604225844225.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNzAzNg==,size_16,color_FFFFFF,t_70)
可惜结果依然是失败。。。

又尝试重启电脑,结果毫无乱用。

最后通过查阅各种资料,得知原因可能是由于电脑的防火墙或者其他网络原因导致ssh连接方式 端口22被封锁。

解决

最终发现两个解决方案

方法一:抛弃ssh连接方式,使用http连接。(我没有用)

操作方法:

  1. 输入命令:
git config --local -e
  1. 将配置文件的url = git@github.com:username/repo.git一行改为:url = https://github.com/username/repo.git
    ![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200604230538249.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNzAzNg==,size_16,color_FFFFFF,t_70)

方法二:如果22号端口不行,那就换一个端口

操作方法:

  1. 进入~/.ssh下
cd ~/.ssh
  1. 创建一个config文件(这里我用的vim编辑器)
vim config
  1. 编辑文件内容:
Host github.com
User git
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

Host gitlab.com
Hostname altssh.gitlab.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
  1. 保存退出
  2. 检查是否成功
ssh -T git@github.com

额,这里要根据它的提示操作,有个地方要输入yes

大功告成,这时候再试试git push,已经可以提交了!

posted @ 2020-06-04 23:16  trafalgar999  阅读(5474)  评论(0)    收藏  举报