Git日志出现fork/main分支但实际无仓库分支的问题排查
问题与解决方法
问题描述
- Git日志中出现
fork/main分支,但本地仅存在1个main分支,GitHub私有仓库显示Fork数为0,不清楚该分支的性质及清除方式。 - 执行
gh pr create命令时,仓库会重复显示为推送分支选项,其他私有仓库无此问题。
相关命令输出
Git日志
~ git log commit abc123abc123 (HEAD -> main, origin/main, origin/HEAD) Author: Me ... commit xyz789xyz789 (fork/main) Author: Me ...
gh pr create 选项重复情况
? Where should we push the 'testing' branch? [Use arrows to move, type to filter] > me/my-private-repo me/my-private-repo Skip pushing the branch Cancel
远程仓库与分支信息
~ git remote -vv fork https://github.com/me/my-repo.git (fetch) fork https://github.com/me/my-repo.git (push) origin https://github.com/me/my-repos-old-name.git (fetch) origin https://github.com/me/my-repos-old-name.git (push) ~ git branch -r fork/fix-types <--- 该PR已合并并删除分支,但从未涉及任何复刻仓库(至少我不知情) fork/main origin/HEAD -> origin/main origin/main
原因分析
fork/main分支的本质:这不是复刻仓库的分支,而是你本地Git添加了一个名为fork的远程仓库,fork/main是该远程仓库main分支的本地追踪副本。从远程信息可以看到,fork指向的是你当前的仓库地址,而origin指向的是仓库旧名称的地址,两者实际对应同一个GitHub仓库。- 重复选项的原因:GitHub CLI识别到两个远程仓库(
origin和fork)对应同一个GitHub仓库,因此在gh pr create时重复显示该仓库选项。
解决步骤
1. 删除多余的fork远程仓库
执行命令删除名为fork的远程:
git remote remove fork
2. 清理本地缓存的远程分支
删除远程后,本地仍会残留追踪分支的缓存,执行以下命令清理:
git fetch --prune
该命令会移除所有远程已不存在的本地追踪分支,包括fork/main和fork/fix-types。
3. 修复重复选项问题
完成上述两步后,再执行gh pr create就不会出现重复的仓库选项了。
可选:更新origin远程地址
由于origin指向的是仓库旧名称地址,建议更新为当前正确地址:
git remote set-url origin https://github.com/me/my-repo.git
内容的提问来源于stack exchange,提问作者Naji
相关产品推荐
相关产品推荐

