Primary Parent Number When Cherry Merges

Suppose this is my git story

  Z
  /
 A - C - D
  \ /      
   B

My HEAD is currently in Z I want to select B and C If my understanding is correct, I must do this:

 git cherry-pick B git cherry-pick C -m 1 git commit --allow-empty 

This worked in my case because C is a non-op (hence, empty commit afterwards, I needed a commit for other reasons), but I wonder what this option does after -m . Here is what I read from docs :

-m parent-number

- parent's main number

Usually you cannot use cherry merge because you don’t know which side of the merger should be considered the main line. This option indicates the parent number (starting from 1) of the main line and allows the cherry plant to reproduce the change relative to the specified parent.

In my case, C has two parents, but how do I know which one is 1 and 2, and more importantly, when it matters, when I choose 1 or 2?

+7
git git-cherry-pick cherry-pick
source share
2 answers

In my understanding, based on this answer , is that parent 1 is a child branch and parent 2 is a child branch. So in your case, parent 1 is A , and parent 2 is B Since cherry pick really applies the difference between the two commits, you use -m 1 to apply only the changes from B (since the difference between A and C contains the changes from B ). In your case, this probably doesn't matter, since you don't have commits between A and C

So yes, -m 1 is what you want, and it's true even if there were additional commits between A and C

If you want the new story to look a little bigger than the original story, there is another way to do this:

 git cherry-pick B git checkout Z git merge --no-ff --no-commit B git commit --author="Some Dev < someone@example.com >" --date="<commit C author date>" 

(If necessary, you can create a new branch for B before choosing a cherry.)

This will save the author’s information, should give you a story that looks like this:

  B' / \ Z -- C' / A -- C -- D \ / B 
+8
source share

I returned Scott Weldon's answer , which is correct, but I just want to add a try to ASCII art, which includes parental numbering. Given a graph that looks like this:

  B / \ ...--A D--... \ / C 

we can say that node D is a merge commit, but we cannot determine if B or C first parent of D One of them is necessarily the first parent, and the second second. Therefore, if we need to know, we must designate a drawing that takes up more space. Here is one such attempt.

  B / \² ...--A D--... \ /¹ C 

Now we see that, for some reason 1, I plotted the “upside down” graph: this commit C is actually the first parent of D , and commit B is the second parent.

It is possible to create arbitrary mergers using lower-level commands ("plumbing"). In particular, git commit-tree simply accepts any many -p arguments you want to give in the order you give them, and makes a new commit with these commits as its parents. Give him one -p and he will commit normally with one parent. Give it the -p arguments and it will make a root commit. Give it 155 different -p arguments (all should, of course, allow valid commit identifiers), and this makes one massive agreement with osynos.

However, the git merge command always makes its new commit with the first parent being the current HEAD (hence the current branch, if on the branch). The second parent element for the standard two parent merge is from .git/MERGE_HEAD , into which git merge writes a different commit identifier. If the merge conflicts or the final merge is completed with --no-commit , this MERGE_HEAD file is actually the only place where the commit identifier is available.

(When git merge merges octopus using the -s octopus strategy - this strategy is enforced for such mergers and several additional parents, it is interrupted and leaves no traces at all if there are merge conflicts, so a conflict case never occurs. I have not tried combining --no-commit with merging octopuses, but that would logically leave parents 2nd through N'th in MERGE_HEAD if Git allows this at all.)


1 stubbornness.

+1
source share

All Articles