为何`git --allow-unrelated-histories`无效?合并仓库丢失提交历史
合并Git仓库丢失提交历史的问题排查与解决
核心原因
git log --follow 仅能追踪单个文件在同一仓库内的重命名/移动历史,而你是将旧仓库的脚本合并到新仓库的bin/目录下,属于跨仓库的路径迁移,默认无法直接关联旧仓库的原始提交。另外,使用--no-commit暂停合并会导致Git未完成历史链的关联,也会影响历史查询。
解决步骤
1. 完成完整的合并流程
先终止未完成的合并,重新执行完整的合并操作:
# 若处于合并暂停状态,先中止 git merge --abort # 移除残留的旧仓库远程(如有) git remote remove old-repo # 重新添加并拉取旧仓库 git remote add old-repo ../old-repo git fetch old-repo # 执行合并并完成提交 git merge old-repo/master --allow-unrelated-histories # 若有冲突,解决后提交:git add . && git commit -m "Merge old-repo scripts"
2. 正确查询跨仓库历史
合并完成后,可通过以下方式查看完整历史:
- 直接查看旧仓库分支下的原始文件历史:
git log old-repo/master -- old-repo-script - 查看新仓库当前路径的合并后历史:
git log -- bin/old-repo-script
3. 让--follow生效的前置操作
若要使用--follow追踪路径变更后的完整历史,需先在旧仓库中完成路径适配提交:
# 进入旧仓库,调整脚本路径与新仓库一致 cd ../old-repo mkdir -p bin mv old-repo-script bin/ git add . git commit -m "Adjust script path to match new repo structure" # 返回新仓库重新合并 cd ../new-repo git fetch old-repo git merge old-repo/master --allow-unrelated-histories # 此时用--follow可追踪完整历史 git log --follow bin/old-repo-script
4. 验证提交是否被拉取
若仍看不到历史,先确认旧仓库的提交已同步到新仓库:
# 查看旧仓库分支的所有提交 git log old-repo/master # 检查目标脚本是否存在于该分支 git show old-repo/master:bin/old-repo-script
注意事项
- Git 2.34.1版本完全支持
--allow-unrelated-histories,版本无问题 - 合并冲突必须解决并提交,否则历史链无法建立
- 避免长期使用
--no-commit暂停合并,这会导致Git处于异常状态
内容的提问来源于stack exchange,提问作者Michael Scheper
相关产品推荐
相关产品推荐

