git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Fwd: Idea: add --squash to cherry-pick
       [not found] <CAO8o=D6p+aFOeRdTDHwPa1E+V45Uqh-TuSZbz0WU=rE-ksGj_w@mail.gmail.com>
@ 2022-08-11  8:53 ` Noam Yorav-Raphael
  2022-08-11 15:42   ` Johannes Sixt
  0 siblings, 1 reply; 4+ messages in thread
From: Noam Yorav-Raphael @ 2022-08-11  8:53 UTC (permalink / raw)
  To: git

Hi,

Here's something I need regularly, which I haven't found an easy
solution for yet. If there's support, I may try and implement it.

The need is described in this Stack Overflow question:
https://stackoverflow.com/q/35123108. It's fairly popular (107 votes
for the question, 154 for the best answer), but I find the suggested
solution lacking.

Basically, I would like to add a flag --squash to the cherry-pick
command, that would apply the diff between the start and end of the
specified range in one operation.

The use case is that there's a feature branch which I would like to
apply on another branch as one commit.

I could use the `-n` flag to apply all the commits from the source
branch without committing them. However, if there are conflicts, I
would have to deal with them on every commit applied. Instead, what I
want is to just apply the diff between the first and last commit, and
then deal with the conflicts.

I find this to be a very natural operation. Usual cherry-pick applies
the difference between commit A^ and commit A over HEAD. The suggested
`git cherry-pick --squash A..B` would apply the difference between
commit A and commit B over HEAD.

What do you think?

Thanks,
Noam

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fwd: Idea: add --squash to cherry-pick
  2022-08-11  8:53 ` Fwd: Idea: add --squash to cherry-pick Noam Yorav-Raphael
@ 2022-08-11 15:42   ` Johannes Sixt
  2022-08-11 16:43     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Sixt @ 2022-08-11 15:42 UTC (permalink / raw)
  To: Noam Yorav-Raphael; +Cc: git

Am 11.08.22 um 10:53 schrieb Noam Yorav-Raphael:
> The need is described in this Stack Overflow question:
> https://stackoverflow.com/q/35123108. It's fairly popular (107 votes
> for the question, 154 for the best answer), but I find the suggested
> solution lacking.
> 
> Basically, I would like to add a flag --squash to the cherry-pick
> command, that would apply the diff between the start and end of the
> specified range in one operation.
> 
> The use case is that there's a feature branch which I would like to
> apply on another branch as one commit.
> 
> I could use the `-n` flag to apply all the commits from the source
> branch without committing them. However, if there are conflicts, I
> would have to deal with them on every commit applied. Instead, what I
> want is to just apply the diff between the first and last commit, and
> then deal with the conflicts.
> 
> I find this to be a very natural operation. Usual cherry-pick applies
> the difference between commit A^ and commit A over HEAD. The suggested
> `git cherry-pick --squash A..B` would apply the difference between
> commit A and commit B over HEAD.

That question on Stackoverflow asks "how to do X with Y". But Y (git
cherry-pick) is the wrong tool to do X (apply commits from a branch to
somewhere else in squashed form).

   git diff A...B | git apply --3way

would do what you want. You would have to come up with a new commit
message anyway, so cherry-pick would be of little use there.

-- Hannes

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fwd: Idea: add --squash to cherry-pick
  2022-08-11 15:42   ` Johannes Sixt
@ 2022-08-11 16:43     ` Junio C Hamano
  2022-08-12  8:37       ` Noam Yorav-Raphael
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2022-08-11 16:43 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Noam Yorav-Raphael, git

Johannes Sixt <j6t@kdbg.org> writes:

> That question on Stackoverflow asks "how to do X with Y". But Y (git
> cherry-pick) is the wrong tool to do X (apply commits from a branch to
> somewhere else in squashed form).
>
>    git diff A...B | git apply --3way
>
> would do what you want. You would have to come up with a new commit
> message anyway, so cherry-pick would be of little use there.

I do agree that cherry-pick is not the best solution for that XY
problem, but in a typical undisciplined development work, it is not
entirely implausible that pieces from original commit message may
still be of some use, exactly the same way as people would want to
use "squash" while "rebase -i".  Most commits may have completely
unusable "oops fix", "worked more", etc. messages, but some of them
may summarize what the past handful steps with such garbage messages
have done, which may be worth salvaging.

If I were solving the XY problem, I would probably use "rebase -i".

To transplant the range A..B of the history on top of HEAD, for
example, I'd start with (notice ^0 after B, as I do not trust myself
so I'd leave the true branch B untouched as I may make mistakes
while running rebase):

    $ git checkout --detach HEAD	;# this is only to use @{-1} later
    $ git rebase -i --onto HEAD A B^0

Then if my goal is to squash everything down into a single commit,
then replace all 'pick', except for the first one, to 'squash'.
That will give me one single chance to edit a single commit message,
but the editor buffer starts with the log message from all of the
original, so I can pick good bits from them while writing new stuf.

I'll have the result on detached HEAD.  If I like the result, I may
update the branch I was originally on with it.

    $ git checkout -B @{-1}

Or, if I don't, perhaps because I made mistakes, then I can just
discard it and go back to the original branch.

    $ git checkout @{-1}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fwd: Idea: add --squash to cherry-pick
  2022-08-11 16:43     ` Junio C Hamano
@ 2022-08-12  8:37       ` Noam Yorav-Raphael
  0 siblings, 0 replies; 4+ messages in thread
From: Noam Yorav-Raphael @ 2022-08-12  8:37 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Sixt, git

Thank Hannes and Junio for your suggestions!

Another issue with using "git diff A...B | git apply --3way", besides
it not showing the commit messages, is that I wouldn't be able to use
a graphical merge tool that shows the entire source file. When I use
"git cherry-pick" and there are conflicts, I see the change in the
context of the entire file.

(I'm using this modification I made of the meld diff tool, which shows
4 panes, and helps me understand the cause of the conflict:
https://github.com/noamraph/meld)

My main problem with using "rebase -i" is that it would require me to
fix merge conflicts one by one, on each commit in which they appear,
instead of fixing all conflicts at once, treating the change from A to
B as one. It also requires manual editing for every commit between A
and B.

I think that the best way to do what I want using the existing commands is:

git checkout A
git merge --squash B
git commit --no-edit
git checkout @{2}  # Go back to where we were at the beginning. This
is not exact, as you're in detached HEAD state.
git cherry-pick --edit @{1}  # cherry-pick the squashed commit A..B

This allows you to fix the merge conflicts in one go, shows the entire
files causing the conflicts, and allows you to edit the commit
message, starting with the descriptions of all the squashed commits.

I think this also gives a pretty good explanation of what "cherry-pick
--squash" will do: it really is the analog of the "merge --squash",
but for cherry-pick.

What do you think? Did I explain my need to "cherry-pick --squash" and
explain its behavior?

If I do an experiment and try to implement this, would this help?

Thanks,
Noam

On Thu, Aug 11, 2022 at 7:43 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Johannes Sixt <j6t@kdbg.org> writes:
>
> > That question on Stackoverflow asks "how to do X with Y". But Y (git
> > cherry-pick) is the wrong tool to do X (apply commits from a branch to
> > somewhere else in squashed form).
> >
> >    git diff A...B | git apply --3way
> >
> > would do what you want. You would have to come up with a new commit
> > message anyway, so cherry-pick would be of little use there.
>
> I do agree that cherry-pick is not the best solution for that XY
> problem, but in a typical undisciplined development work, it is not
> entirely implausible that pieces from original commit message may
> still be of some use, exactly the same way as people would want to
> use "squash" while "rebase -i".  Most commits may have completely
> unusable "oops fix", "worked more", etc. messages, but some of them
> may summarize what the past handful steps with such garbage messages
> have done, which may be worth salvaging.
>
> If I were solving the XY problem, I would probably use "rebase -i".
>
> To transplant the range A..B of the history on top of HEAD, for
> example, I'd start with (notice ^0 after B, as I do not trust myself
> so I'd leave the true branch B untouched as I may make mistakes
> while running rebase):
>
>     $ git checkout --detach HEAD        ;# this is only to use @{-1} later
>     $ git rebase -i --onto HEAD A B^0
>
> Then if my goal is to squash everything down into a single commit,
> then replace all 'pick', except for the first one, to 'squash'.
> That will give me one single chance to edit a single commit message,
> but the editor buffer starts with the log message from all of the
> original, so I can pick good bits from them while writing new stuf.
>
> I'll have the result on detached HEAD.  If I like the result, I may
> update the branch I was originally on with it.
>
>     $ git checkout -B @{-1}
>
> Or, if I don't, perhaps because I made mistakes, then I can just
> discard it and go back to the original branch.
>
>     $ git checkout @{-1}
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-12  8:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAO8o=D6p+aFOeRdTDHwPa1E+V45Uqh-TuSZbz0WU=rE-ksGj_w@mail.gmail.com>
2022-08-11  8:53 ` Fwd: Idea: add --squash to cherry-pick Noam Yorav-Raphael
2022-08-11 15:42   ` Johannes Sixt
2022-08-11 16:43     ` Junio C Hamano
2022-08-12  8:37       ` Noam Yorav-Raphael

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).