在不存在的文件中进行更改的 Cherry-pick

Cherry-pick with changes in a non-existent file

我有两个架构相似的回购协议:

repo1:
 - file1
 - file2 *(this file is non-existent in repo2)
 - folder
    - file3
    - file4
    - file5

repo2:
 - file1
 - folder
    - file3
    - file4
    - file5

Repo1 对于 repo2 是远程的:

git remote add repo1 http://repo1.git

我需要挑选从 repo1 到 repo2 的提交

git cherry-pick <commit_repo1>

通常一切正常。但是如果我想挑选对不存在的文件的更改,我会遇到问题。

变化如下:

 folder/file4    | 9 ---------
 folder/file5    | 5 -----
 file1           | 5 -----
 file2           | 5 -----
 4 files changed, 24 deletions(-)

最后:Cherry-pick 合并从不存在的文件更改为 file3。仅用于删除更改

如果需要的文件不存在,有人知道如何避免将更改合并到错误的文件吗?

尝试次数:

git cherry-pick --strategy-option theirs <commit_repo1>
git cherry-pick --strategy-option ours <commit_repo1>

给出相同的结果:

Auto-merging folder/file3 !!! this file was not changes (instead changed in file2)
Auto-merging folder/file4
Auto-merging file1
Auto-merging folder/file3

这看起来像是一个过于急切的重命名检测案例:Git 认为 file3 是丢失文件的匹配项,因此它将更改导入那里。

您可以禁用重命名检测:

-X no-renames

(即 git cherry-pick -X no-renames <em>hash</em>,而不是 -X ours-X theirs) 来避免这个问题。或者,使用 git cherry-pick -n 以便 Git 不会 提交 结果,然后通过检查 HEAD 版本修复编辑的文件:

git cherry-pick -n <hash>

检查结果,进行任何所需的更改,包括:

git checkout HEAD -- folder/file3

最终:

git commit

进行新的提交。

为什么会得到这个结果

挑选提交时,您必须查看提交引入的更改

根据您的描述,看起来 file2 出现在父提交中,并且 git 以某种方式计算出 file2 已重命名为 folder/file3

因此:尽职尽责地尝试应用该更改的樱桃采摘。


如何解决这个问题

您可以告诉 git 完全忽略重命名(正如@torek 回答的那样):

git cherry-pick -X no-renames

您可能会在 folder/file3 上发生冲突(现在 git 因为您正在导入创建 folder/file3 的提交,但它已经存在于您的目标分支上),其中你可以修复。

或者您可以在挑选之后手动解决手头的问题:

# it looks like you want to revert `file3` to its previous state :
git checkout HEAD^ -- folder/file3
git commit --amend