首页/开发/Git 与协作速查/git clone --single-branch 单分支克隆
Git 基础与命令88 条

git clone --single-branch 单分支克隆

只抓取指定分支的引用,进一步压缩传输数据;默认与 --depth 组合使用。后续需要其他分支时可调整 remote 的 refspec 再拉取。

Git 与协作速查开发更新于 2026-10-12
用途只要一个分支
参数--single-branch [--branch <名称>]
恢复多分支git remote set-branches origin '*'

常用示例

常用示例
git clone --single-branch -b main https://example.com/org/repo.git
git remote set-branches origin '*'
git fetch origin
只读构建场景(CI/发布机)用单分支克隆可以少拉大量引用,注意这正是「只拿需要的」而不是功能受限。

易错点

  • 单分支克隆后 git branch -r 只能看到该分支,误以为远端没有别的分支
  • 用 --branch 指定标签名也会触发 detached HEAD 检出,切换前确认分支状态

数据来源与参考

  • Git 官方文档:git-clone(--single-branch)
  • Git 官方文档:git-remote(set-branches)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌