Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

huangapple go评论69阅读模式
英文:

Git `cherry-pick` adds lines from commits previous to the cherry-picked commits

问题

目标: 我有一个 Git 存储库中的 main 分支和 somebranch 分支。我想将somebranch 中的最后一次提交应用到 main 分支。为此,我尝试使用 cherry-pick 命令。

Git 树的结构如下所示:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

文件树如下所示:

/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 提交更改如下:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

added bar 提交更改如下:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

我使用的命令如下:

git checkout main
git cherry-pick <added bar 提交的 ID>

但是,与我预期的简单地添加 added bar 的修改不同,它显示了以下合并冲突窗口:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

第 10 行的 foo("hello") 是属于 added foo 的修改,但并没有包含在我的 cherry-pick 中。那么为什么这些修改会出现在这个合并工具中? added barsome 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:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

and file tree looks like:

/src
  main.py

Before the cherry-pick, main.py looks like this:

def foo(s):
    # Foo says...
    print(f&#39;Foo says {s}&#39;)

def bar(s):
    # Bar says... 
    print(f&#39;Bar says {s}&#39;)

if __name__ == &quot;__main__&quot;:
    pass

added foo does this:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

and added bar:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

The command I used:

git checkout main
git cherry-pick &lt;added bar commit id&gt;

But... instead of git simply adding the modifications I expected, ie, adding bar, it shows the following merge conflict window:

Git的`cherry-pick`命令会将先前的提交中的行添加到被挑选的提交中。

line 10 foo(&quot;hello&quot;) 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.

huangapple
  • 本文由 发表于 2023年7月7日 03:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定