首页/开发/Git 与协作速查/git clone --depth 浅克隆
Git 基础与命令88 条

git clone --depth 浅克隆

浅克隆只取有限条提交:--depth=N 控制深度,明显减少拉取体积与时间,适合 CI 与只需要新近代码的场景;代价是历史不完整。

Git 与协作速查开发更新于 2026-10-12
用途限制克隆深度、减少耗时
语法git clone --depth <N> <URL>
补历史git fetch --unshallow 或 --depth 加深

常用示例

常用示例
git clone --depth 1 https://github.com/org/repo.git
git clone --depth 50 --no-single-branch https://github.com/org/repo.git
git fetch --depth 200      # 在浅克隆里继续加深
git fetch --unshallow      # 补全为完整仓库
浅克隆下 git log/blame/bisect 与合并基线分析可能不准确;需要评审历史或定位回归时先补全历史。

易错点

  • 默认 --depth 隐含 --single-branch,需要多分支历史时要显式加 --no-single-branch
  • 在浅克隆上直接变基/合并到旧基线可能失败,先 fetch --unshallow 再操作

数据来源与参考

  • Git 官方文档:git-clone(--depth)
  • Git 官方文档:git-fetch(--unshallow/--depth)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌