解决 Git 克隆报错 RPC failed; curl 56 Recv failure: Connection reset
分析 Git clone 大仓库时报错 RPC failed curl 56 诱因,详解 http.postBuffer 调优与浅克隆 depth 技巧。
问题现象
在尝试克隆大型开源项目或推送庞大的提交历史时,进度走到 80%~90% 突然断开,报错:
error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was non-properly terminated.
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
或者在 Windows / macOS 上提示:
error: RPC failed; curl 56 Recv failure: Connection reset by peer
fatal: the remote end hung up unexpectedly
克隆被强行终止,本地工作目录被清空。
触发根因
- Git 默认 HTTP 缓冲区(http.postBuffer)过小:默认配置下,Git 的智能 HTTP 传输协议(Smart HTTP)缓冲区通常仅为 1MB。当推送或拉取包含海量大二进制文件或上万次 commit 的包时,缓冲区溢出导致分包重传超时。
- 跨国高延迟链路在长时间下载中发生 TCP 假死断连:由于代码库体积高达数 GB,单次 HTTP 连接持续时间过长,沿途运营商或代理网关切断了空闲或大吞吐的 TCP 会话。
- 服务端内存/超时熔断:GitLab 或 GitHub 服务器在生成 pack-file 时超时,主动向客户端下发了 RST 终止信号。
诊断排查步骤
1. 查看当前 Git 的 postBuffer 配置
git config --get http.postBuffer
若为空,说明使用的是默认的 1MB 保守设定。
解决方案
方案一:调大 Git 全局 postBuffer 缓冲区大小(即刻见效)
将 Git 的 HTTP 缓冲区扩充至 500MB 或 1GB:
# 将 postBuffer 增加到 500MB (524288000 字节)
git config --global http.postBuffer 524288000
# 优化压缩级别(降低服务端与客户端的 CPU 打包压力)
git config --global core.compression 0
方案二:使用浅克隆(Shallow Clone --depth 1)极速下载(强烈推荐)
如果只是为了本地开发、构建部署或临时查看代码,无需下载项目完整的十年历史:
# 仅拉取最新一次提交(体积通常从几 GB 骤降至几十 MB,秒级完成)
git clone --depth 1 https://github.com/example/big-repo.git
克隆完成后,若后续确实需要完整提交历史,可在本地按需单步追溯:
# 追溯最近 20 次提交
git fetch --depth=20
# 或者在网络良好时彻底转为完整仓库(Unshallow)
git fetch --unshallow
方案三:改用 SSH 协议替代 HTTP 协议
SSH 协议原生基于双向长连接数据流,没有 HTTP/HTTPS 复杂的头限制与网关超时截断机制:
git clone git@github.com:example/big-repo.git
验证方法
再次执行克隆命令:
git clone --depth 1 https://github.com/example/big-repo.git
控制台平稳完成解包索引:
Receiving objects: 100% (4521/4521), 25.14 MiB | 5.20 MiB/s, done.
Resolving deltas: 100% (1820/1820), done.
无任何 RPC failed 报错,代码完整成功检出。


