Hi Harald,
On Fri, Dec 4, 2026 at 4:51 AM Harald Nordgren via GitGitGadget
<gitg…@gmail.com> wrote:
>
> From: Harald Nordgren <hara…@gmail.com>
>
> "git history squash origin/main..HEAD" fails with "the revision range
> is empty" once a fetch has brought new commits into origin/main, even
> though "git rev-list origin/main..HEAD" still lists the commits on the
> branch. The squash only goes through when the range is spelled as
> "$(git merge-base origin/main HEAD)..HEAD".
>
> The range is walked with --ancestry-path, which keeps only the
> descendants of <base> so that a side branch that forked before <base>
> and was merged later is left out. Commits on a branch that forked from
> an ancestor of <base> are not its descendants, so none of them are
> kept.
Ok, this part makes sense: with --ancestry-path and the report I gave,
we'd get an empty range (since "origin..D" selects C and D, but
ancestry-path removes them as non-ancestors/non-descendants of
origin). I don't know that I understand why we use --ancestry-path,
though (see below) the <base>..<tip> special-casing might have
something to do with it? I see Patrick suggested that in the original
discussion to avoid commits not descended from <base>, which leads
directly to the feedback I gave, I suppose.
> Treat <base> as the commit where <tip> forked from it, so that both
> spellings squash the same commits.
This part I'm not following so much: are you saying that, given
"origin..HEAD" (for example), we should resolve that to the merge-base
version instead? I suppose that makes sense from an implementation
perspective, perhaps.
> A <base> that is an ancestor of
> <tip> is its own fork point and such a range is squashed as before.
Right, we shouldn't break the existing code.
I'll admit I'm not terribly familiar with the revision walk machinery,
but this all feels like quite a lot of mechanism for a (somewhat)
simple policy. To me, "git history squash" should take a revision
range (= set of commits) and:
• check that the set of commits is connected, has one "bottom
[ancestor] commit", and one [descendant] tip; and
• collapse the set down to one commit, applying on the bottom's parent.
Just going by the docs, I see some nods in this direction, although I
find it a bit strange in retrospect that the docs use terms "above"
and "below" (which I don't think we often use elsewhere when
describing commit topology---none of the occurrences in git-log(1) are
about commit ordering that I can see).
I can see how the current behavior is implied by
> The range is given in the usual <base>..<tip> form, where <base> is
> the commit just below the oldest commit to squash.
but that really does exclude the reported case, and doesn't match with
how I think of the set of commits referred to by <base>..<tip> in
general. In fact, we accept commands like
git history squash ^origin HEAD
too, so I don't see why the docs or implementation seem to want to
give special weight to <base>.
I'd be happy to learn what I'm missing! Maybe our implementation is
pretty close to the bullets I described above already, in which case
with the bugfix + some doc changes we're in better shape. But right
now it seems like we're doing a lot of work to treat <base> specially,
when I think we should either
• Let folks use (some parts of) the revision range machinery to
specify the set of commits however they want, and let Git figure out
the what the bottom + parent of new commit is, or
• Make the interface more explicit, like "git history squash <base>
<tip>" or something.
I think the former is more convenient, personally.
>
> Reported-by: D. Ben Knoble <ben…@gmail.com>
> Signed-off-by: Harald Nordgren <hara…@gmail.com>
> ---
> history: squash a branch whose base has moved on
>
> Fix git history squash origin/main..HEAD failing with "the revision
> range is empty" after a fetch has moved origin/main past the commit the
> branch forked from.
>
> Reported in
> https://lore.kernel.org/git/CALnO6CCOyVX_Setv4fy3sUnccQn889dHhUa=NyoO…@mail.gmail.com/
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2515%2FHaraldNordgren%2Fhistory-squash-diverged-base-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2515/HaraldNordgren/history-squash-diverged-base-v1
> Pull-Request: https://github.com/git/git/pull/2515
>
> Documentation/git-history.adoc | 5 +++-
> builtin/history.c | 50 +++++++++++++++++++++++++++++++---
> t/t3455-history-squash.sh | 26 ++++++++++++++++++
> 3 files changed, 76 insertions(+), 5 deletions(-)
>
> diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
> index 1fcc30150a..291920d61b 100644
> --- a/Documentation/git-history.adoc
> +++ b/Documentation/git-history.adoc
> @@ -128,7 +128,10 @@ The range is given in the usual `<base>..<tip>` form, where _<base>_ is
> the commit just below the oldest commit to squash. For example, `git
> history squash HEAD~3..HEAD` folds the three most recent commits into
> one, and `git history squash HEAD~5..HEAD~2` squashes an interior range
> -while leaving the two newest commits in place. Several revisions may be
> +while leaving the two newest commits in place. _<base>_ may also be a
> +branch that has gained commits since _<tip>_ forked from it, so `git
> +history squash origin/main..HEAD` folds the commits made since the fork
> +even after a fetch has updated `origin/main`. Several revisions may be
> given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is
> already on `topic`. Rev-list options may also be given, but any that would
> change how the range is walked are overridden with a warning.
In other words, I think we should try to simplify this paragraph and
the implementation (if possible): it shouldn't be necessary to explain
that a range r1..r2 works to select the commits on r2 but not r1 ;)
> diff --git a/builtin/history.c b/builtin/history.c
> index 43ba76f5a1..c848526368 100644
> --- a/builtin/history.c
> +++ b/builtin/history.c
> @@ -1318,10 +1318,42 @@ static int build_squash_message(struct repository *repo,
> const struct strbuf *todo_buf,
> struct strbuf *out);
>
> +/*
> + * The walk keeps only the descendants of its bottom commits, and <base> has
> + * none in the range when the tip forked from one of its ancestors. Also walk
> + * from the merge bases of <base> and each tip so that such a range is not
> + * empty.
> + */
> +static int add_merge_base_bottoms(struct repository *repo,
> + struct rev_info *revs,
> + struct commit *base)
> +{
> + for (size_t i = 0; i < revs->cmdline.nr; i++) {
> + struct rev_cmdline_entry *arg = &revs->cmdline.rev[i];
> + struct commit_list *merge_bases = NULL;
> + struct commit *tip;
> +
> + if (arg->flags & UNINTERESTING)
> + continue;
> + tip = lookup_commit_reference_gently(repo, &arg->item->oid, 1);
> + if (!tip)
> + continue;
> + if (repo_get_merge_bases(repo, base, tip, &merge_bases) < 0)
> + return -1;
> + for (struct commit_list *p = merge_bases; p; p = p->next)
> + commit_list_insert(p->item,
> + &revs->ancestry_path_bottoms);
> + commit_list_free(merge_bases);
> + }
> + return 0;
> +}
> +
> static int setup_squash_revisions(struct repository *repo,
> int argc, const char **argv,
> struct rev_info *revs)
> {
> + bool has_base = false;
> +
> repo_init_revisions(repo, revs, NULL);
> revs->reverse = 1;
> revs->topo_order = 1;
> @@ -1357,11 +1389,21 @@ static int setup_squash_revisions(struct repository *repo,
> * something, as in "<base>..<tip>". A revision range with no such
> * bottom commit cannot be squashed.
> */
> - for (size_t i = 0; i < revs->cmdline.nr; i++)
> - if (revs->cmdline.rev[i].flags & BOTTOM)
> - return 0;
> + for (size_t i = 0; i < revs->cmdline.nr; i++) {
> + struct rev_cmdline_entry *arg = &revs->cmdline.rev[i];
> + struct commit *base;
>
> - return error(_("not a '<base>..<tip>' revision range"));
> + if (!(arg->flags & BOTTOM))
> + continue;
> + has_base = true;
> + base = lookup_commit_reference_gently(repo, &arg->item->oid, 1);
> + if (base && add_merge_base_bottoms(repo, revs, base) < 0)
> + return -1;
> + }
> + if (!has_base)
> + return error(_("not a '<base>..<tip>' revision range"));
> +
> + return 0;
> }
>
> /*
> diff --git a/t/t3455-history-squash.sh b/t/t3455-history-squash.sh
> index 237afdfa26..643d005984 100755
> --- a/t/t3455-history-squash.sh
> +++ b/t/t3455-history-squash.sh
> @@ -264,6 +264,32 @@ test_expect_success 'squashes when the base is the root commit' '
> test "$tip_tree" = "$(git rev-parse HEAD^{tree})"
> '
>
> +test_expect_success 'squashes onto the fork point when the base has moved on' '
> + test_when_finished "rm -rf upstream downstream" &&
> + git init upstream &&
> + (
> + cd upstream &&
> + test_commit base
> + ) &&
> + git clone --single-branch upstream downstream &&
> + (
> + cd upstream &&
> + test_commit upstream-moved
> + ) &&
> + (
> + cd downstream &&
> + test_commit one &&
> + test_commit two &&
> + git fetch &&
> + git history squash --no-edit origin..HEAD &&
> + check_log_subjects HEAD <<-\EOF &&
> + one
> + base
> + EOF
> + test_cmp_rev two^{tree} HEAD^{tree}
> + )
> +'
> +
> test_expect_success 'squashing a mix of fixups' '
> git reset --hard three &&
> echo fix >file &&
>
> base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
> --
> gitgitgadget
--
D. Ben Knoble