0

I am trying to cherry pick a commit from src-branch to dst-branch.

for example I have a commit that changes file1.txt:

file1.txt in src-branch before commit:

hello world!
goodbye world!

file1.txt in src-branch after commit:

hello world!
goodbye world!

new changes here!

file1.txt in dst-branch before performing cherry-pick:

hello world!

file1.txt in dst-branch after performing cherry-pick:

hello world!
goodbye world!

new changes here!

But I expect that the file after the cherry pick will look like this (whithout goodbye world!):

hello world!

new changes here!

because the line goodbye world! was not included in the commit changes, and as far as I know cherry-pick should only apply the changes of the specific commit.

I would like to now why this happends, and how can I prevent it. Thank you.

Noy Gafni
  • 1,091
  • 9
  • 19
  • Please add more information to your question: (1) the exact cherry-pick command used, and also (2) the output of `git log --oneline --grapp src-branch dst-branch`. – Inigo Dec 19 '21 at 12:31
  • 2
    You're not describing correctly what each commit looks like. If the file had really looked as you describe in each commit, the cherry-pick would have resulted in a merge conflict. – matt Dec 19 '21 at 12:37
  • Can you also add what `git show ` outputs, for the commit you are cherry-picking? I'd like to see the actual diff that Git says that commit represents. – joanis Dec 19 '21 at 14:06
  • 1
    I agree with matt : "file1.txt in dst-branch after performing cherry-pick:" is not the expected result of the previous actions, it should have conflicted. – Romain Valeri Dec 19 '21 at 15:52

1 Answers1

2

You're not describing correctly what each commit looks like. (Your use of the phrase "before commit" and "after commit" makes me suspect that the reason for this is that you are not clear on what Git is or what commits are.) If the file had really looked as you describe in each commit, the cherry-pick would have resulted in a merge conflict. That is the expected result from the situation you describe.

I'll prove it. Here's a starting topology:

* 92a9f15 (HEAD -> dst) irrelevant, still hello world
| * dedbf7e (main) new changes here
| * d466d6d goodbye world
|/  
* d2d99ae hello world

Now I'll look at file1.txt in every commit, and you will see that it is exactly as you describe:

###
% git show d2d99ae:file1.txt
hello world!

####
% git show d466d6d:file1.txt
hello world!
goodbye world!

###
% git show dedbf7e:file1.txt
hello world!
goodbye world!

new changes here!

###
% git show HEAD:file1.txt
hello world!

Now I'll perform the cherry-pick:

% git cherry-pick dedbf7e

The result is a merge conflict. And rightly: Git cannot coherently turn

hello world!
goodbye world!

into both

hello world!
goodbye world!

new changes here!

and

hello world!

which is what you are asking it to do.

matt
  • 515,959
  • 87
  • 875
  • 1,141