如何使用自签名证书克隆 GitLab 仓库

发布: (2026年2月1日 GMT+8 16:52)
3 分钟阅读
原文: Dev.to

Source: Dev.to

Problem Overview

在使用自签名 SSL 证书的 GitLab 实例时,通过 HTTPS 克隆常会出现如下错误:

fatal: unable to access 'https://gitlab.example.com/group/project.git/': 
server certificate verification failed. CAfile: none CRLfile: none

Git 默认不信任自签名证书,除非显式告诉 Git 信任该证书,否则 HTTPS 连接会被拒绝。

Export the Self‑Signed Certificate

Using Chrome

  1. 在 Chrome 中打开你的 GitLab 地址:https://gitlab.example.com

  2. 点击锁形图标 → Connection is secureCertificate is valid

  3. 切换到 Details 选项卡 → Export…

  4. 将证书保存为 gitlab-selfsigned.crt,放在永久位置:

    • Windows: C:\certs\gitlab-selfsigned.crt
    • Linux/macOS: /home/username/certs/gitlab-selfsigned.crt

Using Firefox

  1. 在 Firefox 中打开 about:preferences#privacy
  2. 滚动到 Certificates → 点击 View Certificates
  3. Servers 选项卡中找到 gitlab.example.com,选中后点击 Export…
  4. 将文件保存为 gitlab-selfsigned.crt

Using OpenSSL (any platform)

openssl s_client -connect gitlab.example.com:443 -showcerts 2>/dev/null \
| openssl x509 -outform PEM > gitlab-selfsigned.crt

该命令直接从 GitLab 获取证书并写入当前目录下的 gitlab-selfsigned.crt

Configure Git to Trust the Certificate

# Windows example
git config --global http.sslCAInfo "C:/certs/gitlab-selfsigned.crt"

# Linux/macOS example
git config --global http.sslCAInfo "/home/username/certs/gitlab-selfsigned.crt"

Verify the Setting

git config --global --get http.sslCAInfo

该命令应输出你刚配置的路径。

Clone the Repository Over HTTPS

git clone https://gitlab.example.com/group/project.git

在信任证书后,克隆应能顺利完成,不再出现 SSL 错误。

Use SSH as an Alternative (No Certificate Issues)

Generate an SSH key

ssh-keygen -t ed25519 -C "your_email@example.com"

Add the public key

~/.ssh/id_ed25519.pub 添加到 GitLab,路径为 User Settings → SSH Keys → Add Key

Clone via SSH

git clone git@gitlab.example.com:group/project.git
git config --global http.sslVerify false

⚠️ Warning: 禁用验证不安全,会让你容易受到中间人攻击。仅在短期排查问题时使用,切勿在生产环境中使用。

Configuring Git in PyCharm

  1. 打开 File → Settings → Version Control → Git
  2. 确认 Path to Git executable 指向你已配置的 Git。
  3. 点击 Test 以确保可执行文件工作正常。
  4. 如果使用 HTTPS,确保 PyCharm 使用的 Git 已应用相同的 http.sslCAInfo 设置(或改用 SSH)。
  1. 导出自签名证书(Chrome、Firefox 或 OpenSSL)。
  2. 使用 http.sslCAInfo 全局配置 Git,指向导出的 .crt 文件。
  3. 通过 HTTPS 克隆仓库,或改用 SSH 完全避免证书处理。
  4. 绝不要 长期关闭 http.sslVerify
Back to Blog

相关文章

阅读更多 »