git push出现“ssh: connect to host github.com port 22: Connection timed out ”

前几天使用git push上传的时候出现下述问题:
在这里插入图片描述
百度搜索之后总共有两种解决方案:一、将ssh方式改为https方式,即关联远端仓库的时候换一种方式,这种方式的配置在~/.git/config的文件中更改。二、将ssh的端口改掉,即在~/.ssh/config的文件中更改。

这两种方式在我的电脑只有第二种能够成功,第一种还是会出现错误。这里我简单记录一下:
第一种:将ssh方式更改为https方式。

  • 找到~/.git/config,可以使用命令:git config --local -e,出现下述内容:
[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = [email protected]:Norah2/norah2.github.io.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	name = nora
	email = [email protected]
  • 将其中的url = [email protected]:Norah2/norah2.github.io.git改为url = https://github.com/Norah2/norah2.github.io.git,其实就是将原来的git@更改为https://,再将冒号:改为斜杠/。如果实在不会改的话,可以登录到github上,进入到远端的仓库中,点击code之后选择https,直接复制上面的链接即可。

更改完成之后保存退出,我再次尝试上传的时候仍旧出现了问题,所以我尝试了第二种方式。(第一种没有成功可能是因为没有刷新配置,后面使用第二种方式成功了,所以没有继续往下测试)

第二种:更改ssh的端口。

  • 新建一个~/.git/config文件:vim ~/.git/config
  • 在里面新增内容:
Host github.com
User [email protected]
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

在第二行的User后面填上自己的邮箱,填好之后保存退出。

  • 需要重新使用命令git config user.name xxx以及git config user.email xxx 设置一下,为了刷新配置用的。

在这里插入图片描述

如果没有这一步的话git push仍旧会出问题:
在这里插入图片描述

  • 可以使用命令ssh -T [email protected]检测ssh是否正常:
    在这里插入图片描述
    上述操作完成之后,再次git push上传才恢复正常:
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42992706/article/details/128753025