threads / discuss / 47908

RFC: git squash

Subject: RFC: git squash

## tl;dr

4 messages between Feb 23, 2018 and Feb 23, 2018.

replies: 3people: 2as markdown or json

Julius Musseau· Feb 23, 2018, 03:41 UTC · lore
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
Junio C Hamano· Feb 23, 2018, 06:04 UTC · re: Julius Musseau · lore

Re: RFC: git squash

Julius Musseau <julius@mergebase.com> writes:
Show 15 quoted lines
> 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.

Julius Musseau· Feb 23, 2018, 17:03 UTC · re: Junio C Hamano · lore

Re: RFC: git squash

On Thu, Feb 22, 2018 at 10:04 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 27 quoted lines
> 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?
Junio C Hamano· Feb 23, 2018, 20:31 UTC · re: Julius Musseau · lore

Re: RFC: git squash

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).

← back to recent threads