Article View: linux.kernel.git
Article #4318[PATCH] history: squash a branch whose base has moved on
From: "Harald Nordgren via GitGitGadge...
Date: Fri, 04 Dec 2026 09:51
Date: Fri, 04 Dec 2026 09:51
170 lines
16899 bytes
16899 bytes
From: Harald Nordgren <haraldnordgren@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.
Treat <base> as the commit where <tip> forked from it, so that both
spellings squash the same commits. A <base> that is an ancestor of
<tip> is its own fork point and such a range is squashed as before.
Reported-by: D. Ben Knoble <ben.knoble@gmail.com>
Signed-off-by: Harald Nordgren <haraldnordgren@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=NyoOLyXbe768XA@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.
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
Technical Details
Message-ID:
<pull.2515.git.git.1796377873326.gitgitgadget@gmail.com>Path:
mbox-mo1.pugleaf.net!news-can.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-76985-linux-kernel-git=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-can.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-76985-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Harald Nordgren via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH] history: squash a branch whose base has moved on
Date: Fri, 04 Dec 2026 09:51:13 +0000
Organization: Usenet Network
Message-ID: <pull.2515.git.git.1796377873326.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2730265"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:+MRs0yu+qNEMLhHHHYlTqXSMUOs= sha256:SuBYHKN4cnC0tnYigTo9asVZmmTYvcJxITgZm5ZocuQ=
sha1:uwAao5wTDp59JQA5qnuVqZhuQmU= sha256:r5pkmC/1ZfI7vNj0P40B2qDvFwbsE8oL59S0L3imuAI=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id CF89C117B8A; Fri, 4 Dec 2026 12:51:30 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.196
X-Spam-Status: No, score=-2.196 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1,
DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.001,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 9xA3McRNA-Yu;
Fri, 4 Dec 2026 12:51:27 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76985-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 483E8117B52; Fri, 4 Dec 2026 12:51:18 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 1FF0026442; Fri, 4 Dec 2026 09:51:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1796377877;
b=oUOCv77GpPvl8rFMVjdZfQWmPTpdvmlnPjRUqYYYHxJMV47dw9NNa+2RLjalWw8MSevO
uWDRKZk9hkZkfSnJydRxDqmyBVGJu3bX0khdG4HN2fNp2OCrav+EDJ5k9s4o7GgAkvL58
csoOrSwZIM1oYtNFrrMi7TFpTZXqRHVHmN8hbbPjqa8XMITX6c8+R5nw1IL36NrhtNwVj
xu5f/jLokjk/eW5CpCtHVJBcSPCF/ybUGQta7m3O9DPR13Yx8mh1o3smRw5l3WVyJ6PYD
IVcrZ4jiLLpUSlWlpIj/aOuROEvza1MYTHlvmUbJTBbajhsHFaVeDDt0vqWRRncLs+g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1796377877;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:Message-Id:From:Date:Subject:Fcc:Content-Type:
Content-Transfer-Encoding:X-Mailing-List:List-Id:List-Post:List-Help:
List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:To:
Cc;
bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=;
b=KJBJhTABoMul4dapKkbyW1h5C0V98wchHAIdneJzXXMlaXOU97UMaviMsj9MMjiYuWrb
zS0+xKyEgFc8TTVtsxTXk48MD9sH3NVilotNMN4K2UfLIiNpSnOVO38G2KT63EbPJJfP0
kfzUlIJ1FG2G7SbQDY6iUZPZ1e5nfhO+rv4Fhek7bWVqlv+tRFONT4xKg0eUNIruIHGNU
96VMyzq2eHHOzQ7u4ZMOcHt24g/MjZo/JIocpJNuwvyhWnE7ZVhJvadmML7FsijjAb9Rb
0IxRz0GpTB/CsMomHfthPyJEyMiIBSIFZdj/OlOKYrNX25g7bWlAkOCjZYfj4GBiGUw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fXWjhKt1;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id E8683606A32D;
Fri, 4 Dec 2026 09:51:16 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fXWjhKt1
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.175
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1796377876;
b=xY7NdVNRpRiUtUTh9fFF4d+ZxRe/2+R4ffTQ/o89deZe1FQFLqZ+O4o9kPlLFDd76vYm
CKRTdj7akDbLr3f2UFX9Vu3HgJ+VLo/c67Xnq0YZLUMlASMtdqkwSCQLC6Kt2+mRmGuyN
Ku6htgoPMkDFBpruOFqrLxc8JzNYueOl3p02VeyTfshebjFKN3vJJ9RSAM/L3SbPmrurf
P2hscGi1sDSyfhZ+WYWcyTDi/k/O4j0wscFKe/FDa2lM9HsMNgn/Xc2lwxlBWoy8Eb53p
y+r0BrTOKh1UKX8AB4zbvCst1Ro5x4S37NbrQB7YAF1HVe9OHBNIPuSPk7IaRA+vqcQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1796377876;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:From:Date:
Subject:Fcc:Content-Type:Content-Transfer-Encoding:MIME-Version:To:
Cc;
bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=;
b=hIF2gxU7/tAsURs6XRrcmCyT8tqOHc4K58lGdYj5ielT74uM7Nhr733SpOgs9deWCHru
ZnRmf4BfwyfO6eQnZNKgrpKoDveHlvmsvEkcMohmTC1BRkpPE7BKApGT4peFIi2W8NyFC
oKxxgecp7jQ1YPEj0ygfNa9DUu2c71Pm3cgjWcC5lcIwJfnSI39n97z1z8eceTP0EcNbO
+n2Q+UU4/zSPjJUR+b2gUHQQsaErvQCd/KvtSXC6BPhlpwdMCjaSIYJK9IEmLdbpIELfC
0RQk5XZ45h7uEsiQTJOirf7lb9eqfiGSPx8plNFHc2J55txKjbQjVIbj5meh7arb9bw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fXWjhKt1;
arc=none smtp.remote-ip=209.85.215.175
X-Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6720606A322
for <git@vger.kernel.org>; Fri, 4 Dec 2026 09:51:15 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B6720606A322
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-cdb2dfcca94so389861a12.1
for <git@vger.kernel.org>; Fri, 04 Dec 2026 01:51:15 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1796377875; x=1796982675; darn=vger.kernel.org;
h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
:subject:date:from:message-id:from:to:cc:subject:date:message-id
:reply-to:content-type;
bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=;
b=fXWjhKt1naF2GM2c6FB1liPhEhIWljxH+AxJJP+ExU3tTRiXMl/dZJnEsyk7AWlyyZ
/DXJcT1Euntrrd5TecHBubEOKlYhbk+jqmkpFe5DXs2B2sYrb26UoHZ4ybQRW8Px+26K
Xl8vPUFxENVjr+ERfekj9NTL3MsFLoWIiGFOdwd/7hHvRvzgQadPl+vI6+riEvqpOIz9
Hjb4zr28e7+AAE0bbfOE/9XQhER7O4MZsyxrg+VryWzazWUMl04AjQKvh53veyTMhmyJ
+II3X6yHN4PFYYg1ftVsLwUX6LqmCBGrW8RoR17gdShZe2efDhMoFp+RRUkdKrBORtpK
4fXw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1796377875; x=1796982675;
h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
:subject:date:from:message-id:x-gm-gg:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=xXI6mKOrB4jmj6p5/R5Au5S231Ve1fpqNhL1snQHSm4=;
b=XGZjiD+3cKlADPk4KVJgRhafKAqDyDxu16/ZpUckBZKDpxksDcbF+eyyBtV8XBgXaX
3K6IW8AMABfEX3uw90YMEXLTionrwwO8Gd0G/nx3PwELOFt07nJ82wXKW4pReofqnCxU
exmld0fpAQD8zaHdFlW2VV7ZABwbvMIf5kDADdmMqfdWIeGMG5HzDl37cqOIJcRb6mWR
C7lwwK4iGx/9LbBG4G5qo/Sie3nCYm5FfGYhch2WBAePbVaS7puL6MGzwQSmE9kgMHV+
GoW+UQwAGFcA+Wdd5LnIJLRbpVJk5Tgvim7OD3TPeEY/OX0MuDIWC528W49La9nZhM7N
bJnw==
X-Gm-Message-State: APuU4Z9rgaRavfhZCfKCIICW5PAey+lqYIKRgRK9JOzon/yduBht+Ko/
FQQlI4/5wzba7TN/9ZBJX5bumCq5HlG8z1iOZyHmxQ/OcmYa80sDzbAK+eGBPg==
X-Gm-Gg: AZFdTZfHoxObGEn10OLvHIBocUMyVyhIB/6ECKl2RyV7YhZoDXpwBfKIjo9wq52gPZJ
zfBaWvGWe7eWlw/hgrID2HU0CiaTnks1VfocKGCbe6utMUN6Vonze2bPUDvliVhFN7/pAqITBjV
aYPbv9rvD05FU+FwI2Ho22redCm8JpAl67WAIoPSWj6CiMo8XOEp+2vBDy72QBpRNUYXQyfj40q
Y9cDTDQuTGHduThvNx5Qsqy7AYi6+5Zn6MSPEynKJg7b1ekrRPsDhwds4qcCB99VmQdrbf9vJc0
HvuXqL0hxoFW/19vKpM4fwwu7RqFuootYJnHTd8xkF4kreRv/WW7O5FMOJuRs0GdFKYHcLY/zui
VKRobRHOtkjuI7s+3UKy9JSWmZwzb93Xx+tyPZ205fZm3CcyLFArVI7tnfbZQ1DDcX5yR/yGaso
vlDRdxillltmaSRLbPXWqWYxiyKy9GURBkzdihrERC8k3ZpPCO7C84ylsNEL3T5L4e9Tei9w5n/
0QRuJW/kaEG1bruBJApXzBrJJTIDy6P0j3KXm5UkFJHh+pQpNB9XNsdxYUkRZp4RXvsOo0mz34a
rPdH8tM8uWt3w5KCqr0LTmujpwoI07aXFw==
X-Received: by 2002:a05:6a21:1f85:b0:3e2:9dee:42cd with SMTP id adf61e73a8af0-415619382famr1596579637.36.1796377874765;
Fri, 04 Dec 2026 01:51:14 -0800 (PST)
X-Received: from [127.0.0.1] ([172.149.32.178])
by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ce12356536dsm1006972a12.9.2026.12.04.01.51.14
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 04 Dec 2026 01:51:14 -0800 (PST)
X-Fcc: Sent
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-Cc: "D. Ben Knoble" <ben.knoble@gmail.com>,
Phillip Wood <phillip.wood123@gmail.com>,
Patrick Steinhardt <ps@pks.im>,
Harald Nordgren <haraldnordgren@gmail.com>,
Harald Nordgren <haraldnordgren@gmail.com>