git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Johannes Sixt <j6t@kdbg.org>
Cc: Noam Yorav-Raphael <noamraph@gmail.com>, git@vger.kernel.org
Subject: Re: Fwd: Idea: add --squash to cherry-pick
Date: Thu, 11 Aug 2022 09:43:10 -0700	[thread overview]
Message-ID: <xmqqzggag28x.fsf@gitster.g> (raw)
In-Reply-To: <a4c937ed-ea89-0564-db64-f0634fe7eeee@kdbg.org> (Johannes Sixt's message of "Thu, 11 Aug 2022 17:42:15 +0200")

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}


  reply	other threads:[~2022-08-11 17:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2022-08-12  8:37       ` Noam Yorav-Raphael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqzggag28x.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    --cc=noamraph@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).