-2

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:

enter image description here

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:

enter image description here

and added bar:

enter image description here

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:

enter image description here

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.

Bersan
  • 1,032
  • 1
  • 17
  • 28
  • 1
    "added bar" adds the line `bar("there")` after the line `foo("hello")`, but the line `foo("hello")` is not there when you try to cherry-pick "added bar", so it's a conflict which you have to tell Git how to solve. – mkrieger1 Jul 06 '23 at 19:31
  • 3
    Does this answer your question? [What are the reasons and cases that cause git merge conflicts?](https://stackoverflow.com/questions/44359334/what-are-the-reasons-and-cases-that-cause-git-merge-conflicts) – mkrieger1 Jul 06 '23 at 19:32
  • Note that the title is misleading. cherry-pick does *not* just add lines from previous commits (because that would be wrong), instead it *asks* you whether they should be added or not. – mkrieger1 Jul 06 '23 at 19:33
  • @mkrieger1 I read it 3 times, it does not answer my question. Feel free to point out where this extensive post answers my question. It is talking about all the cases for merge conflict, between 2 branches. My question is not about merge conflict of two branches (the conflict between `main` and `somebranch` is not relevant to my question, I would still have the same issue had `main` not changed `line 10`). Rather is about why `cherry-pick` places changes from commit `added bar` when i picked commit `added foo` only. – Bersan Jul 08 '23 at 09:52
  • @mkrieger1 the closest thing your link gets to answering my question would be this point: *Similarly, if one side adds a line above or below a line that the other side deletes, you get a conflict [...]*. However, I am not deleting lines, only adding lines. This could answer my question if he was talking about `cherry-pick`-ing commits that add lines above lines that do not exist yet on the base branch. – Bersan Jul 08 '23 at 10:27

1 Answers1

1

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.
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.

Jay
  • 3,640
  • 12
  • 17
  • That makes sense. However, I'm still confused as to how many lines will git look for context over. For instance, I tried adding 5 extra lines to the `added foo` commit, and below those lines, the `added bar` line. [This picture](https://imgur.com/a/B8te37T) shows that all lines from `added foo` appeared at the `cherry-pick` conflict resolution tool when I picked `added bar` – Bersan Jul 08 '23 at 14:30
  • 1
    Well, I guess context is just relevant for matching the target location and deciding whether the change can be added cleanly. But once a conflict is detected, git (or the merge-tool) will scan and present the complete chunks of differences, which may extend further out than the context that was considered for the previous step. – Jay Jul 09 '23 at 15:31