英文:
Git `cherry-pick` adds lines from commits previous to the cherry-picked commits
问题
目标: 我有一个 Git 存储库中的 main
分支和 somebranch
分支。我想将somebranch
中的最后一次提交应用到 main
分支。为此,我尝试使用 cherry-pick
命令。
Git 树的结构如下所示:
文件树如下所示:
/src
main.py
在执行 cherry-pick 之前,main.py
的内容如下:
def foo(s):
# Foo says...
print(f'Foo says {s}')
def bar(s):
# Bar says...
print(f'Bar says {s}')
if __name__ == "__main__":
pass
added foo
提交更改如下:
而 added bar
提交更改如下:
我使用的命令如下:
git checkout main
git cherry-pick <added bar 提交的 ID>
但是,与我预期的简单地添加 added bar
的修改不同,它显示了以下合并冲突窗口:
第 10 行的 foo("hello")
是属于 added foo
的修改,但并没有包含在我的 cherry-pick
中。那么为什么这些修改会出现在这个合并工具中? added bar
和 some commit
中的修改以黄色高亮显示,这正是我实际希望显示的内容。
英文:
Objective: I have a main
branch and a somebranch
in a git repo. I want to apply only the last commit from somebranch
to main
. To do that, I try to use cherry-pick
.
The git tree looks like this:
and file tree looks like:
/src
main.py
Before the cherry-pick, main.py
looks like this:
def foo(s):
# Foo says...
print(f'Foo says {s}')
def bar(s):
# Bar says...
print(f'Bar says {s}')
if __name__ == "__main__":
pass
added foo
does this:
and added bar
:
The command I used:
git checkout main
git cherry-pick <added bar commit id>
But... instead of git simply adding the modifications I expected, ie, adding bar, it shows the following merge conflict window:
line 10 foo("hello")
is a modification pertaining to added foo
, which is not included in my cherry-pick
. So why are these modifications showing up in this merge tool? The modifications from added bar
and some commit
appear highlighted in yellow, and are what I would actually expect to have showing up, only.
答案1
得分: 1
GIT通常考虑每个更改行上下的3行“上下文”。
(例如,为了确定一行/更改的位置,因为行号可能不同)
如果这些上下文行有任何差异,则会产生冲突。
在你的情况下,git试图在“main”和“bar”之间插入“foo”。
但由于目标中没有“bar”,它将其标记为冲突,因为git无法确定这是否是正确的位置。
另外,git无法确定“foo”是否需要“bar”才能正确工作。
解决冲突的方法是保留“foo”,但放弃“bar”,如果这确实是技术上正确/有效的解决方案。
英文:
GIT usually considers 3 lines of "context" above and below for each changed line.
(e.g. for figuring out where a line/change goes, as the line number can be different)
If there are any differences in those context lines, then there will be a conflict.
In your case git is trying to insert "foo" between "main" and "bar".
But because there is no "bar" in the destination, it marks it as a conflict, because git can't be sure if that's the right place.<br>Also git cannot know whether or not "bar" is required for "foo" to work correctly.
Your solution to the conflict would be to keep "foo" but discard "bar", if that is indeed the technical correct/working solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论