如何将源仓库指定文件的提交Cherry-pick到目标仓库根目录
解决跨仓库Cherry-pick时调整文件路径的问题
你遇到的核心问题是源仓库的目标文件嵌套在linux/目录下,直接cherry-pick会把整个目录结构带过来。下面提供两种实用的解决方案:
方法一:Cherry-pick后手动调整路径(适合少量提交)
- 保持你已完成的远程配置,执行带
--no-commit参数的cherry-pick,将提交内容暂存但不自动提交:
git cherry-pick -n <sha-start>^..<sha-end>
- 将
linux/下的目标文件移动到目标仓库根目录:
git mv linux/scripts_folder . git mv linux/notes.bash .
- 删除空的
linux/目录:
git rm -r linux
- 检查工作区文件结构符合预期后,用原提交信息完成提交:
git commit -C <sha-end>
注意:如果cherry-pick的提交中包含其他不需要的文件,先执行
git reset HEAD linux/无关文件,删除本地无关文件后再提交。
方法二:生成并修改补丁后应用(适合批量提交)
- 生成目标提交区间的补丁文件:
git format-patch <sha-start>^..<sha-end> --stdout > temp.patch
- 编辑
temp.patch文件,将所有路径中的linux/前缀删除(比如把--- a/linux/scripts_folder/script1.sh改为--- a/scripts_folder/script1.sh,+++ b/linux/scripts_folder/script1.sh改为+++ b/scripts_folder/script1.sh)。 - 应用修改后的补丁:
git am temp.patch
- 清理临时补丁文件:
rm temp.patch
内容的提问来源于stack exchange,提问作者VikSil
相关产品推荐
相关产品推荐

