git format-patch结合git am跨仓应用补丁的文件冲突问题
Git跨仓库补丁迁移保留独立提交记录问题
我需要从仓库repo-a生成补丁并合并至另一仓库repo-b,要求补丁保留完整的独立提交记录,经调研git format-patch与git am的命令组合非常适配该需求。
我在repo-a中采用多分支开发模式,时常出现两个不同分支分别新增同名文件后合并至main分支的场景,但该场景下执行git am应用补丁会触发报错,完整问题复现步骤如下:
mkdir repo-a; cd repo-a git init touch randomfile git add randomfile git commit -m "Adding an initial file" git checkout -b anotherbranch touch a-file git add a-file git commit -m "Adding a-file from anotherbranch" git checkout main touch a-file git add a-file git commit -m "Adding a-file from main" git merge anotherbranch git format-patch -o patches HEAD~2 # patches/0001-Adding-a-file-from-anotherbranch.patch # patches/0002-Adding-a-file-from-main.patch cd .. mkdir repo-b; cd repo-b git init cp -r ../repo-a/patches ./ git am patches/* # Applying: Adding a-file from anotherbranch # applying to an empty history # Applying: Adding a-file from main # error: a-file: already exists in index # Patch failed at 0002 Adding a-file from main # hint: Use 'git am --show-current-patch=diff' to see the failed patch # When you have resolved this problem, run "git am --continue". # If you prefer to skip this patch, run "git am --skip" instead. # To restore the original branch and stop patching, run "git am --abort".
上述操作失败的原因为Git索引中已存在同名文件a-file,无冲突的常规场景下git am可正常应用补丁并保留提交记录。
现咨询是否存在可行方案,可在保留所有独立提交记录(不合并为单个大补丁)的前提下完成跨仓库补丁迁移?我已查阅git format-patch与git am的官方说明文档,尝试过--find-copies-harder等可选参数,但均未解决问题。
补充背景信息
repo-b是通过cp -r复制repo-a,执行rm -rf .git and-some-other-files后重新执行git init生成的主仓库,repo-a是裁剪后的精简版本。- 该操作的核心目标是实现权限隔离:允许相关人员在裁剪后的
repo-a上开展开发工作,无需接触repo-b的全量内容;从repo-b复制生成repo-a的过程中仅执行文件删除操作,无其他内容修改。
- 该操作的核心目标是实现权限隔离:允许相关人员在裁剪后的
内容的提问来源于stack exchange,提问作者xeor
相关产品推荐
相关产品推荐

