解决 Git 访问 GitHub 时的 SSL 错误

在使用 Git 访问 GitHub 仓库时,有时会遇到 SSL 错误,这通常是由于 SSL 证书问题、网络设置问题或者 Git 配置问题引起的。本文将详细介绍解决这些问题的方法,帮助您顺利访问 GitHub。

一、常见的 SSL 错误及原因

  1. SSL certificate problem: self signed certificate这是由于 SSL 证书不被信任或是自签名证书引起的。
  2. SSL certificate problem: unable to get local issuer certificate这是由于无法验证 SSL 证书链中的一个或多个证书引起的。
  3. fatal: unable to access 'https://github.com/...': SSL certificate problem: certificate has expired这是由于 SSL 证书已过期引起的。

二、解决方法

1. 更新 CA 证书

更新系统的 CA 证书,确保本地的证书链是最新的。

复制代码
# 对于Debian/Ubuntu
sudo apt-get update
sudo apt-get install --reinstall ca-certificates

# 对于RedHat/CentOS
sudo yum update ca-certificates
​
2. 配置 Git 使用系统的 CA 证书

通过设置 Git 配置,指定 CA 证书文件的位置。

复制代码
git config --global http.sslCAInfo /etc/ssl/certs/ca-certificates.crt
3. 禁用 SSL 验证(不推荐)

如果以上方法无效,您可以暂时禁用 SSL 验证以解决紧急问题,但这会降低安全性,不推荐长期使用。

复制代码
git config --global http.sslVerify false
4. 使用 SSH 方式访问 GitHub

将 GitHub 仓库地址从 HTTPS 改为 SSH,可以绕过 SSL 验证问题。

复制代码
git remote set-url origin git@github.com:username/repository.git

然后配置 SSH 密钥:

  1. 生成 SSH 密钥

    复制代码
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  2. 添加 SSH 密钥到 ssh-agent

    复制代码
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
  3. 将 SSH 公钥添加到 GitHub 账户

    复制公钥内容并添加到 GitHub 账户的 SSH keys 设置中:

    复制代码
    cat ~/.ssh/id_rsa.pub

三、验证解决方案

1. 测试 Git 连接

使用以下命令测试 Git 连接:

复制代码
git ls-remote https://github.com/username/repository.git

或使用 SSH:

复制代码
git ls-remote git@github.com:username/repository.git
2. 确认配置

确保 Git 配置已经正确应用:

复制代码
git config --list
相关推荐
fthux23 分钟前
待办清单写满了,别忘了给休息留个位置:试试 Tick Rest
开源·github·mac·提醒·open source·消息提醒·tick rest·tick rest for mac·消息提醒app
GitHub实测笔记2 小时前
dust 本机实测:/usr 扫树中位 92ms、du -sh 80ms;-s 不是汇总,稀疏文件 0→100Mi
github
逛逛GitHub2 小时前
有个刚开源的 GitHub 项目有说法,竟然能在终端里再跑一台 Mac。
github
用户2991422196802 小时前
GitHub 热榜项目:日榜(2026-11-09)
人工智能·开源·github
徐小夕3 小时前
JitArchives,一款基于JitWord协同AI文档打造的企业级电子档案系统
前端·vue.js·github
特创数字科技4 小时前
写了个 Windows 下载器:一键拉 GitHub / Gitee 源码,带 SHA256 校验
python·github
miofly5 小时前
GitHub 日榜趋势速报 | 2026-11-09
开源·github
miofly20 小时前
Nous Research微调Qwen3.8-27B,宣称可剥离中国官方立场内容
开源·github
用户2991422196801 天前
GitHub 热榜项目:周榜(2026-11-08)
人工智能·开源·github
用户2991422196801 天前
GitHub 热榜项目:日榜(2026-11-08)
人工智能·开源·github