git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* RFC: git squash
@ 2018-02-23  3:41 Julius Musseau
  2018-02-23  6:04 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Julius Musseau @ 2018-02-23  3:41 UTC (permalink / raw)
  To: git

Hi, Git Developers,

Thanks for your help regarding my earlier email (trying to break git
pull --rebase).

I just wanted to warn you all that my first attempt at a patch is
imminent.  I'm working on a "git squash" command.  Here's a quick
summary:

------
git squash [<commit>]

Squashes <commit>..HEAD into a single commit. Replaces HEAD with the
result.  If not specified, <commit> defaults to the current branch's
upstream (a.k.a. @{upstream}).

Rationale:

This command provides an intuitive mechanism for in-place squash that
doesn't drop dirty merge results.

We call this an in-place squash because the state of all files and
directories at HEAD does not change. Only the ancestory of HEAD
changes: its (only) parent becomes the merge-base of <commit> and
HEAD, removing all intermediate commits.

Alternatives:

- "git merge --squash master" correctly preserves dirty merge results,
but it's tricky to achieve an in-place squash with this command, since
it requires the following sequence of commands (these assume the
current branch is "feature" and we want to squash it relative to
"master"):

    git checkout $(git merge-base HEAD master)
    git merge --squash feature
    git commit
    git branch -f feature
    git checkout feature

- "git rebase --interactive HEAD~N" with commits set to "squash" (or
"fixup") is very popular in industry.  But it has some tricky edge
cases: drops dirty merge results, can run into conflicts, and can be
confusing if HEAD~N spans any merges (including clean merges).
-----


I expect I'll have the patch finished this weekend, and ready for
everyone to look at by Monday (Feb 26th).

Note:  I'm not 100% sure "git rebase --interactive" would drop dirty
merge results (e.g., the dirty parts from conflict-resolving merges).
That's speculation on my part.  I'll confirm this before I finish and
submit the patch.


yours sincerely,

Julius Musseau

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

* Re: RFC: git squash
  2018-02-23  3:41 RFC: git squash Julius Musseau
@ 2018-02-23  6:04 ` Junio C Hamano
  2018-02-23 17:03   ` Julius Musseau
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2018-02-23  6:04 UTC (permalink / raw)
  To: Julius Musseau; +Cc: git

Julius Musseau <julius@mergebase.com> writes:

> git squash [<commit>]
>
> Squashes <commit>..HEAD into a single commit. Replaces HEAD with the
> result.  If not specified, <commit> defaults to the current branch's
> upstream (a.k.a. @{upstream}).
>
> Rationale:
>
> This command provides an intuitive mechanism for in-place squash that
> doesn't drop dirty merge results.
>
> We call this an in-place squash because the state of all files and
> directories at HEAD does not change. Only the ancestory of HEAD
> changes: its (only) parent becomes the merge-base of <commit> and
> HEAD, removing all intermediate commits.

So is it essentially the same as

    git reset --soft $(git merge-base $commit HEAD)
    git commit

with some icing for coming up with a default log message?  The above
won't touch the working tree at all.



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

* Re: RFC: git squash
  2018-02-23  6:04 ` Junio C Hamano
@ 2018-02-23 17:03   ` Julius Musseau
  2018-02-23 20:31     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Julius Musseau @ 2018-02-23 17:03 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Thu, Feb 22, 2018 at 10:04 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Julius Musseau <julius@mergebase.com> writes:
>
>> git squash [<commit>]
>>
>> Squashes <commit>..HEAD into a single commit. Replaces HEAD with the
>> result.  If not specified, <commit> defaults to the current branch's
>> upstream (a.k.a. @{upstream}).
>>
>> Rationale:
>>
>> This command provides an intuitive mechanism for in-place squash that
>> doesn't drop dirty merge results.
>>
>> We call this an in-place squash because the state of all files and
>> directories at HEAD does not change. Only the ancestory of HEAD
>> changes: its (only) parent becomes the merge-base of <commit> and
>> HEAD, removing all intermediate commits.
>
> So is it essentially the same as
>
>     git reset --soft $(git merge-base $commit HEAD)
>     git commit
>
> with some icing for coming up with a default log message?  The above
> won't touch the working tree at all.
>
>

Yes!  I had no idea about this approach.  Thanks!  My implementation
uses "git commit-tree".

Yes, some icing to build the squashed log message, as well as some
protection to ensure the working tree is clean before doing the squash
(just like "git rebase" does).

I'm also working on two more pieces of icing:

- Adding an "--oldest-author" flag to help adjust the author for the
squashed commit (using the author value from the oldest commit in the
squash).  By "oldest" I mean whatever "git log --reverse --no-merges
-1 <commit>..HEAD" spits out.  (I don't consider timestamps.)

- Ability to arbitrarily squash any branch by taking a 2nd "branch"
argument.  (And making the command work with bare repos.)

I'm embarrassed to realise your approach matches the top-voted
stack-overflow answer on the subject:
https://stackoverflow.com/a/5201642

Nonetheless, the top-voted stack-overflow comment to that answer says:

> Ha! I like this method. It is the one closes to the spirit of the problem. It's a pity that it requires so much voodoo. Something like this should be added to one of the basic commands.

Should I proceed?

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

* Re: RFC: git squash
  2018-02-23 17:03   ` Julius Musseau
@ 2018-02-23 20:31     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2018-02-23 20:31 UTC (permalink / raw)
  To: Julius Musseau; +Cc: git

Julius Musseau <julius@mergebase.com> writes:

> I'm embarrassed to realise your approach matches the top-voted
> stack-overflow answer on the subject:
> https://stackoverflow.com/a/5201642

I personally do not visit stack-overflow, but I am happy to see that
there are people who remember "right" way to do this among the folks
who do.

"reset --soft" was invented exactly for this use case, and you can
use it even with dirty working tree and dirty index (they are left
intact).

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

end of thread, other threads:[~2018-02-23 20:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-23  3:41 RFC: git squash Julius Musseau
2018-02-23  6:04 ` Junio C Hamano
2018-02-23 17:03   ` Julius Musseau
2018-02-23 20:31     ` Junio C Hamano

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 NNTP newsgroup(s).