Lol*_*ola 1 git git-cherry-pick
我有两个具有类似架构的存储库:
repo1:
- file1
- file2 *(this file is non-existent in repo2)
- folder
- file3
- file4
- file5
Run Code Online (Sandbox Code Playgroud)
和
repo2:
- file1
- folder
- file3
- file4
- file5
Run Code Online (Sandbox Code Playgroud)
Repo1 对 repo2 来说是远程的:
git remote add repo1 http://repo1.git
Run Code Online (Sandbox Code Playgroud)
我需要挑选从 repo1 到 repo2 的提交
git cherry-pick <commit_repo1>
Run Code Online (Sandbox Code Playgroud)
通常一切正常。但是,如果我想对不存在的文件进行挑选更改,我就会遇到问题。
变化如下:
folder/file4 | 9 ---------
folder/file5 | 5 -----
file1 | 5 -----
file2 | 5 -----
4 files changed, 24 deletions(-)
Run Code Online (Sandbox Code Playgroud)
最后:Cherry-pick 合并从不存在的文件更改为 file3。仅用于删除更改
如果需要的文件不存在,有人知道如何避免将更改合并到错误的文件中吗?
尝试:
git cherry-pick --strategy-option theirs <commit_repo1>
git cherry-pick --strategy-option ours <commit_repo1>
Run Code Online (Sandbox Code Playgroud)
给出相同的结果:
Auto-merging folder/file3 !!! this file was not changes (instead changed in file2)
Auto-merging folder/file4
Auto-merging file1
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Auto-merging folder/file3
这看起来像是一个过于急切的重命名检测案例:Git 认为file3与丢失的文件匹配,所以它导入了那里的更改。
您可以禁用重命名检测:
-X no-renames
Run Code Online (Sandbox Code Playgroud)
(即 ,而不是或)来避免这个问题。或者,使用以便 Git 不提交结果,然后通过检查版本来修复编辑的文件:git cherry-pick -X no-renames hash-X ours-X theirsgit cherry-pick -nHEAD
git cherry-pick -n <hash>
Run Code Online (Sandbox Code Playgroud)
检查结果,进行任何所需的更改,包括:
git checkout HEAD -- folder/file3
Run Code Online (Sandbox Code Playgroud)
并最终:
git commit
Run Code Online (Sandbox Code Playgroud)
进行新的提交。