在使用 git clone 时遇到的错误:“fetch-pack: unexpected disconnect while reading sideband packet”和“fatal: protocol error: bad pack header”。这通常是由于仓库太大,导致 git 无法在默认的缓冲区大小内处理数据。
- 使用浅克隆的方法,然后在一步步获取完整历史
bash
git clone --depth 1 https://github.com/username/repo.git
cd repo
git fetch --unshallow
- 增加缓冲区大小
bash
git clone --depth 1 --buffer-size=655360 https://github.com/username/repo.git
- 切换 HTTP 协议版本
某些情况下 HTTP/2 协议可能不稳定,可强制使用 HTTP/1.1:
bash
git config --global http.version HTTP/1.1
- 关闭低速传输限制
避免因网络波动导致的中断:
bash
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
分批次下载仓库
浅层克隆 + 逐步拉取完整历史 对于大型仓库,先下载最近一次提交,再逐步补全历史:
Bash
git clone --depth 1 [仓库链接] # 仅克隆最新提交
cd [仓库目录]
git fetch --unshallow # 拉取完整历史(可能需重试)
分阶段增量拉取(适用于持续失败)
若 --unshallow 失败,可逐步增加拉取深度:
Bash
git fetch --depth=100 # 首次拉取 100 次提交
git fetch --depth=500 # 逐步增加深度