用途:诊断并修复 Git 仓库中远程分支不可见的常见问题。
背景:使用 git clone --depth=1 或 git clone --single-branch 克隆仓库时,Git 会把 .git/config 中的 fetch refspec 限制为只拉取默认分支,例如 fetch = +refs/heads/master:refs/remotes/origin/master,导致 git fetch --all 和 git branch -a 只能看到默认分支。问题不在网络或权限,而在本地一行配置。
主要能力与流程:
- 诊断:执行 git branch -a、git config --get remote.origin.fetch、git ls-remote --heads origin,比对本地可见分支与远程实际分支。若 fetch 值不是 +refs/heads/:refs/remotes/origin/,说明被限制为单分支;若 ls-remote 也只有默认分支,则远程确实只有一个分支,无需修复。
- 修复:执行 git config remote.origin.fetch "+refs/heads/:refs/remotes/origin/",把单分支规则改为通配符。
- 拉取:执行 git fetch --all。
- 验证:执行 git branch -a,确认分支出现在 remotes/origin/ 下。
- 可选:浅克隆仅补全了分支引用,历史仍截断;需要完整历史时执行 git fetch --unshallow,可用 git rev-parse --is-shallow-repository 判断是否为浅克隆。
其他排查:若 refspec 已是通配符仍看不到分支,依次检查网络连通性、分支访问权限、执行 git remote update origin --prune 清理缓存、用 git remote -v 确认分支是否在其他 remote。
限制与注意:修改 fetch refspec 是安全操作,不影响已有本地分支或工作树;git fetch --all 只下载引用和相关对象,不会自动创建本地分支或修改工作树;需要在某远程分支工作时可用 git checkout <branch-name> 自动创建追踪分支。资料未提供价格、平台等内容。