git clone 和 git pull 代码无响应

记录一下今天 git 拉代码遇到的一些异常情况;
无论是项目目录下 git pull 还是直接 git clone 都不能正常拉代码;

  • 异常情况1
ssh: connect to host gitee.com port 23: Connection timed out
fatal: Could not read from remote repository.

我把 ssh: connect to host gitee.com port 23: Connection timed out 当做关键词百度了一下,很多文章都让在 ~/.ssh 目录创建个 config 文件;PreferredAuthentications 印象中是在多账户的情况下配置的,还是抱着试一试的想法测试了一下,发现在我这边是无效的。

  Host gitee.com
  User git
  Hostname ssh.gitee.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa
  Port 443

ssh -T [email protected] 测试报错 ssh: Could not resolve hostname ssh.gitee.com: Name or service not known

  • 异常情况2
    针对 ssh -T 响应的错误搜索了一下,不是让修改 host 文件就是给一段 Python 代码;感觉不太对直接跳过了。
    嗯…回到错误1换种思路想一下,既然是报错 connect to host gitee.com port 23: Connection timed out ,说明把本地使用ssh的端口当做 gitee 连接的端口了,那么就换种思路,在使用命令时声明端口呢?
git clone ssh://[email protected]:22/xahy/xahy-001.git

在这里插入图片描述
发现成功拉取代码了。
     
     
虽然ok了,还是思考一个问题;
因为第一次遇到这种问题;
之前使用其他的服务器修改了端口,
拉取代码都是没有任何问题。
复盘之前的操作,发现使用的端口就是当前ssh使用的端口
是不是就是直接使用的当前服务器 ssh 配置声明的端口?
修改 shh 配置文件,验证一下。

vi /etc/ssh/ssh_config
vi /etc/ssh/sshd_config

///以上这2个文件,增加  Port 22

重启ssh服务或者重启服务器

systemctl restart sshd

重新拉取代码,发现ok了
在这里插入图片描述
在修改 ssh 配置时还遇到了 ssh_exchange_identification: Connection closed by remote host错误;
原因是将端口声明为 Port 443了,修改为Port 22就可以了。
不过说明这种方案也是可行的,而且无需特意声明端口。

     
     
总结一下,
遇到 git pull 或者 git clone 无响应;
可以先检查ssh 配置是否声明开启22端口;
或者重新 clone 项目,在 clone 时声明22端口;
就可以成功操作了;
以上两种方案任选一都可以。

     
     

文章到这儿就结束了。

猜你喜欢

转载自blog.csdn.net/xianhenyuan/article/details/91889644
今日推荐