cherry-pick '-m' curiosity
Hello,
$ git help cherry-pick
-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
Is, say, "-m 2" ever useful?
Signature · 1 line
Sergey
Quoted 2 lines · Sergey Organov
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
You may not be merging into the "mainline" in the first place.
Imagine forking two topics at the same commit on the mainline, and merging these two topics of equal importance together into a single bigger topic, before asking the mainline to pull the whole.
$ git checkout mainline
$ git tag fork-point
$ git checkout -b frontend-topic fork-point
$ work work work
$ git checkout -b backend-topic fork-point
$ work work work
$ git checkout -b combined
$ git merge frontend-topic
$ git tag mergedThe backend-topic may be recorded as the first-parent of the resulting merge, but logically the two topics are of equal footing, so merge^1..merge and merge^2..merge are both equally interesting.
Quoted 9 lines · Sergey Organov
Hello,
$ git help cherry-pick-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.
Quoted 4 lines
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
Is, say, "-m 2" ever useful?
Say you want to backport everything except that topic using cherry-picks. Then -m2 would be useful?
Quoted 10 lines · Stefan Beller
Quoted 9 lines · Sergey Organov
Hello,
$ git help cherry-pick-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.
Quoted 7 lines
Quoted 4 lines
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
Is, say, "-m 2" ever useful?
Say you want to backport everything except that topic using cherry-picks. Then -m2 would be useful?
Didn't get the idea, sorry. Care to clarify?
-- Sergey
Quoted 21 lines · Junio C Hamano
Quoted 2 lines · Sergey Organov
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
You may not be merging into the "mainline" in the first place.
Imagine forking two topics at the same commit on the mainline, and merging these two topics of equal importance together into a single bigger topic, before asking the mainline to pull the whole.
$ git checkout mainline $ git tag fork-point $ git checkout -b frontend-topic fork-point $ work work work $ git checkout -b backend-topic fork-point $ work work work $ git checkout -b combined $ git merge frontend-topic $ git tag merged
Quoted 3 lines
The backend-topic may be recorded as the first-parent of the resulting merge, but logically the two topics are of equal footing, so merge^1..merge and merge^2..merge are both equally interesting.
Point taken, thanks! Now, if I reformulate my original question as:
"Isn't it _usually_ the case that "mainline" is the first parent?"
what is the answer?
For example, in the above case I'd likely rather:
$ git checkout -b combined fork-point
$ git merge --no-ff frontend-topic
$ git merge --no-ff backend-topicand still have clear "mainline" on "-m1" for both merges.
I'm asking because those:
"Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline."
in the manual page still feels confusing in the context of typical git usage (as opposed to the context of abstract DAG operations where it'd make sense indeed.)
-- Sergey
Quoted 4 lines · Sergey Organov
Hello,
$ git help cherry-pick
Quoted 8 lines
-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
First-parent will be whatever commit you were sitting on when you typed "git merge".
If you're sitting on your branch and you type "git fetch; git merge origin/master", then "mainline" will be 2nd parent.
Same happens if you type "git pull".
Further reading here:
https://developer.atlassian.com/blog/2016/04/stop-foxtrots-now/
"git revert -m" also has the same problem.
Quoted 4 lines
Is, say, "-m 2" ever useful?
--
Sergey
Quoted 10 lines · G. Sylvie Davies
Quoted 9 lines · Sergey Organov
Hello,
$ git help cherry-pick-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.
Quoted 6 lines
Quoted 3 lines
Isn't it always the case that "mainline" is the first parent, as that's how "git merge" happens to work?
First-parent will be whatever commit you were sitting on when you typed "git merge".
Right, but I believe it's also "mainline", see below.
Quoted 2 lines
If you're sitting on your branch and you type "git fetch; git merge origin/master", then "mainline" will be 2nd parent.
No. If you ever want to cherry-pick this commit, it'd still be -m1 side of it that likely makes sense, and it's exactly the side that makes sense to be picked that is called "mainline" in the manual page we are discussing, and there is no any other definition of "mainline" as far as I can tell.
There is nothing about 'origin/master' that makes it "mainline" from the POV of future cherry-picks, if any, of this merge commit. I was also unable to find any git documentation that calls 'origin/master' "mainline". It's called "remote-tracking branch", or maybe sometimes "upstream".
OTOH, when one merges something, he often merges "side branch" onto "mainline", so in the context of this particular merge, your local "master" happens to be "mainline" and "origin/master" happens to be "side branch".
Quoted 1 line
"git revert -m" also has the same problem.
Yes, as it's essentially just "git cherry-pick --reverse -m", provided cherry-pick has had the "--reverse" from regular "patch" utility [*].
It's also interesting to notice that manual page for "git revert" refers to the revert-a-faulty-merge How-To, that in turn again uses only "git revert -m 1".
Overall, it's still a mystery to me why "-m 1" is not the default behavior for both "git revert" and "git cherry-pick".
The only suspicion I have is that actual intention is to deny picking merge commits by default. Then, the usual git way would be to use --force or --enable-merges to overcome the denial, but if we still do need "-m 2" etc. even rarely, then rather re-using "-m 1" as "I mean it" indication is only logical.
If my suspicion is true, how about something like this:
-m parent-number, --mainline parent-number
This option specifies the parent number (starting from 1) of a commit and instructs cherry-pick to replay the change relative to the specified parent. Cherry-pick will refuse to handle merge commits unless this option is given.
Damn, it now has no "mainline" in the description at all, so it's unclear why it has been called --mainline in the first place, not that it was somehow clear to me before.
And while we are at it, I just stumbled over "git cherry-pick -m 1" refusing to handle non-merge commits:
$ git cherry-pick -m1 dd5c320 error: Mainline was specified but commit dd5c320a300520a044cfa73d17f6cbffbbef60ef is not a merge. fatal: cherry-pick failed $
I wonder whether this is intentional? What's the rationale then? It seems it could be useful to be able to cherry-pick multiple commits, some of which are merges, no?
Footnote:
[*] rebase, revert, and cherry-pick all look rather similar in git and could be calling for some unification.
-- Sergey