如何使用自签名证书克隆 GitLab 仓库
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
-
在 Chrome 中打开你的 GitLab 地址:
https://gitlab.example.com -
点击锁形图标 → Connection is secure → Certificate is valid。
-
切换到 Details 选项卡 → Export…。
-
将证书保存为
gitlab-selfsigned.crt,放在永久位置:- Windows:
C:\certs\gitlab-selfsigned.crt - Linux/macOS:
/home/username/certs/gitlab-selfsigned.crt
- Windows:
Using Firefox
- 在 Firefox 中打开
about:preferences#privacy。 - 滚动到 Certificates → 点击 View Certificates。
- 在 Servers 选项卡中找到
gitlab.example.com,选中后点击 Export…。 - 将文件保存为
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
Temporarily Disabling SSL Verification (Not Recommended)
git config --global http.sslVerify false
⚠️ Warning: 禁用验证不安全,会让你容易受到中间人攻击。仅在短期排查问题时使用,切勿在生产环境中使用。
Configuring Git in PyCharm
- 打开 File → Settings → Version Control → Git。
- 确认 Path to Git executable 指向你已配置的 Git。
- 点击 Test 以确保可执行文件工作正常。
- 如果使用 HTTPS,确保 PyCharm 使用的 Git 已应用相同的
http.sslCAInfo设置(或改用 SSH)。
Recommended Workflow for Self‑Signed GitLab
- 导出自签名证书(Chrome、Firefox 或 OpenSSL)。
- 使用
http.sslCAInfo全局配置 Git,指向导出的.crt文件。 - 通过 HTTPS 克隆仓库,或改用 SSH 完全避免证书处理。
- 绝不要 长期关闭
http.sslVerify。