Cur*_*ous 6 git version-control merge cherry-pick
我正在尝试学习如何使用git cherry pick,我阅读了git通过执行返回的手册页,git cherry-pick --help但似乎没有帮助.我将尝试解释下面的问题.我有两个分支master和other.
在分支上master
提交历史记录是
0x2 Second commit from master branch
0x1 Initial commit
Run Code Online (Sandbox Code Playgroud)
我正在跟踪的存储库中唯一的文件readme具有以下内容
Some text
Run Code Online (Sandbox Code Playgroud)
在分支上other
提交历史记录是
0x5 CHECKPOINT, going to cherry-pick onto master
0x4 second commit from branch other
0x3 first commit from other branch:
0x2 Second commit from master branch
0x1 Initial commit
Run Code Online (Sandbox Code Playgroud)
并且readme文件的内容是
Some text.
Adding some text from other branch. Adding some more text.
Going to cherry-pick this commit.
Run Code Online (Sandbox Code Playgroud)
两个分支上的工作目录都是干净的,没有未跟踪的更改.从这一点开始,当我切换到主分支并与git merge other合并合并时,优雅地发生没有合并冲突.但是当我尝试git cherry-pick 0x5合并冲突时,我得到以下输出git
error: could not apply 0x5... CHECKPOINT, going to cherry-pick onto master
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Run Code Online (Sandbox Code Playgroud)
该readme文件具有以下内容
<<<<<<< HEAD
Some text
=======
Some text.
Adding some text from other branch. Adding some more text.
Going to cherry-pick this commit.
>>>>>>> 0x5... CHECKPOINT, going to cherry-pick onto master
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种合并冲突?我试图理解它为什么会发生.是不是cherry-pick应该尝试对cherry-pick自己编写的提交进行所有编辑,然后将更改提交到分支(master在本例中)?
还有什么时候合并冲突git呢?我似乎在奇怪的时候得到它们.此实现是否依赖(例如,取决于用于合并的算法)?
谢谢!
如果您说merge,则所有三个 (3-5) 提交都会合并。如果你cherry-pick只使用最后一个。它所涉及的文件可能已经在 3 和/或 4 中发生了更改,因此初始阶段与预期不符。
请使用合并工具检查冲突以了解其发生的原因。
例如这会导致冲突:
Original
Original
Original
Commit3
Original
Commit5
Original
Run Code Online (Sandbox Code Playgroud)
这里 git 的 diff 引擎将显示您更改了一行,该行前面有几行,包括来自提交 3 的一行。 Cherry-pick 在原始版本中看不到该行,因此 git 会尖叫冲突并要求您处理它。本身没有冲突的编辑(例如编辑了同一行),git 只是不确定将编辑的部分放在哪里。
当然,您可以根据需要手动编辑文件。只有合并分支才会出现问题和冲突,并且并非所有冲突都意味着“你不能这样做”,它们只是意味着“我不知道到底该怎么做,你自己处理。”