threads / patch / 66932

patchhistory: squash the commits not in the upstream by default

Subject: [PATCH] history: squash the commits not in the upstream by default

## tl;dr

2 messages between Nov 25, 2026 and Dec 1, 2026. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Harald Nordgren via GitGitGadget· Nov 25, 2026, 19:03 UTC · lore
From: Harald Nordgren <haraldnordgren@gmail.com>

"git history squash" with no arguments refuses to run and asks for a revision range, even though the commits to squash are usually the ones on the current branch that its upstream does not have.

The subcommand added in 6fd039c984 (history: add skeleton for squash subcommand, 2026-09-08) started out on the list as "git rebase --squash [<upstream>]", which folded "@{upstream}..HEAD" when given no argument. It moved to "git history" to take any range, and that default was lost in the move.

Squash the commits since the branch forked off its upstream, so this keeps working after a fetch brings in new upstream commits.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    history: squash the commits not in the upstream by default
    
    Make git history squash with no range default to the commits on the
    current branch that are not in its upstream, like git rebase does.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2463%2FHaraldNordgren%2Fhistory-squash-default-upstream-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2463/HaraldNordgren/history-squash-default-upstream-v1
Pull-Request: https://github.com/git/git/pull/2463
 Documentation/git-history.adoc |  8 ++++--
 builtin/history.c              | 49 ++++++++++++++++++++++++++--------
 t/t3455-history-squash.sh      | 31 +++++++++++++++++++--
 3 files changed, 73 insertions(+), 15 deletions(-)
Show changes to 3 files +73 −15

Documentation/git-history.adoc, builtin/history.c, t/t3455-history-squash.sh

diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
index 1fcc30150a..eaf75102d7 100644
--- a/Documentation/git-history.adoc
+++ b/Documentation/git-history.adoc
@@ -12,7 +12,7 @@ git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(
 git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]
 git history reword <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]]
 git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]
-git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] <revision-range>
+git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] [<revision-range>]
 
 DESCRIPTION
 -----------
@@ -117,7 +117,7 @@ linkgit:gitglossary[7].
 It is invalid to select either all or no hunks, as that would lead to
 one of the commits becoming empty.
 
-`squash <revision-range>`::
+`squash [<revision-range>]`::
 	Fold all commits in _<revision-range>_ into the oldest commit of that
 	range. The resulting commit keeps the oldest commit's authorship and
 	takes the tree of the range's newest commit, so the whole range
@@ -133,6 +133,10 @@ 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.
 +
+Without _<revision-range>_, all commits on the current branch that are not in
+its upstream are squashed, starting from the merge base of `HEAD` and
+`@{upstream}`.
++
 An editor opens pre-filled with the messages of all the folded commits so you
 can combine them. With `--no-edit`, the oldest commit's message is preserved
 instead, except that an `amend!` commit targeting it replaces its message.
diff --git a/builtin/history.c b/builtin/history.c
index 43ba76f5a1..0e5c5f2cc8 100644
--- a/builtin/history.c
+++ b/builtin/history.c
@@ -38,7 +38,7 @@
 #define GIT_HISTORY_SPLIT_USAGE \
 	N_("git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]")
 #define GIT_HISTORY_SQUASH_USAGE \
-	N_("git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] <revision-range>")
+	N_("git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] [<revision-range>]")
 
 static void change_data_free(void *util, const char *str UNUSED)
 {
@@ -1319,7 +1319,7 @@ static int build_squash_message(struct repository *repo,
 				struct strbuf *out);
 
 static int setup_squash_revisions(struct repository *repo,
-				  int argc, const char **argv,
+				  struct strvec *args,
 				  struct rev_info *revs)
 {
 	repo_init_revisions(repo, revs, NULL);
@@ -1331,9 +1331,9 @@ static int setup_squash_revisions(struct repository *repo,
 	revs->limited = 1;
 	revs->ancestry_path_implicit_bottoms = 1;
 
-	argc = setup_revisions(argc, argv, revs, NULL);
-	if (argc > 1)
-		return error(_("unrecognized argument: %s"), argv[1]);
+	setup_revisions_from_strvec(args, revs, NULL);
+	if (args->nr > 1)
+		return error(_("unrecognized argument: %s"), args->v[1]);
 
 	if (revs->reverse != 1 || revs->topo_order != 1 ||
 	    revs->sort_order != REV_SORT_IN_GRAPH_ORDER ||
@@ -1372,7 +1372,7 @@ static int setup_squash_revisions(struct repository *repo,
 static int resolve_squash_range(struct repository *repo,
 				bool update_branches,
 				bool edit_message,
-				int argc, const char **argv,
+				struct strvec *args,
 				struct commit **oldest_out,
 				struct commit **tip_out,
 				char **message_out)
@@ -1387,7 +1387,7 @@ static int resolve_squash_range(struct repository *repo,
 	struct ref_array refs = { 0 };
 
 	subject_data.edit_message = edit_message;
-	ret = setup_squash_revisions(repo, argc, argv, &revs);
+	ret = setup_squash_revisions(repo, args, &revs);
 	if (ret < 0)
 		goto out;
 
@@ -1612,6 +1612,28 @@ out:
 	return ret;
 }
 
+static int default_squash_range(struct repository *repo, struct strvec *args)
+{
+	struct commit *upstream, *head;
+	struct commit_list *bases = NULL;
+
+	upstream = lookup_commit_reference_by_name("@{upstream}");
+	if (!upstream)
+		return error(_("could not resolve the upstream of HEAD"));
+	head = lookup_commit_reference_by_name("HEAD");
+	if (!head)
+		return error(_("could not resolve HEAD"));
+	if (repo_get_merge_bases(repo, upstream, head, &bases) < 0)
+		return -1;
+
+	strvec_push(args, "squash");
+	for (struct commit_list *b = bases; b; b = b->next)
+		strvec_pushf(args, "^%s", oid_to_hex(&b->item->object.oid));
+	strvec_push(args, "HEAD");
+	commit_list_free(bases);
+	return 0;
+}
+
 static int cmd_history_squash(int argc,
 			      const char **argv,
 			      const char *prefix,
@@ -1636,6 +1658,7 @@ static int cmd_history_squash(int argc,
 		OPT_HISTORY_GPG_SIGN(&sign_commit),
 		OPT_END(),
 	};
+	struct strvec args = STRVEC_INIT;
 	struct strbuf reflog_msg = STRBUF_INIT;
 	struct commit *oldest, *tip, *rewritten;
 	const struct object_id *base_tree_oid, *tip_tree_oid;
@@ -1647,19 +1670,22 @@ static int cmd_history_squash(int argc,
 	argc = parse_options(argc, argv, prefix, options, usage,
 			     PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0);
 	if (argc < 2) {
-		ret = error(_("command expects a revision range"));
-		goto out;
+		ret = default_squash_range(repo, &args);
+		if (ret < 0)
+			goto out;
+	} else {
+		strvec_pushv(&args, argv);
 	}
 
 	if (action == REF_ACTION_DEFAULT)
 		action = REF_ACTION_BRANCHES;
 
 	strbuf_addstr(&reflog_msg, "squash: updating ");
-	strbuf_join_argv(&reflog_msg, argc - 1, argv + 1, ' ');
+	strbuf_join_argv(&reflog_msg, args.nr - 1, args.v + 1, ' ');
 
 	ret = resolve_squash_range(repo, action == REF_ACTION_BRANCHES,
 				   edit,
-				   argc, argv, &oldest, &tip,
+				   &args, &oldest, &tip,
 				   &message_template);
 	if (ret < 0)
 		goto out;
@@ -1693,6 +1719,7 @@ static int cmd_history_squash(int argc,
 	ret = 0;
 
 out:
+	strvec_clear(&args);
 	strbuf_release(&reflog_msg);
 	release_revisions(&revs);
 	free(message_template);
diff --git a/t/t3455-history-squash.sh b/t/t3455-history-squash.sh
index 237afdfa26..ac33d1cec3 100755
--- a/t/t3455-history-squash.sh
+++ b/t/t3455-history-squash.sh
@@ -111,9 +111,36 @@ test_expect_success 'setup linear history touching two files' '
 		test_commit three file d
 '
 
-test_expect_success 'errors on missing range argument' '
+test_expect_success 'errors on missing range argument without an upstream' '
 	test_must_fail git history squash 2>err &&
-	test_grep "expects a revision range" err
+	test_grep "no upstream configured" err
+'
+
+test_expect_success 'squashes the commits not in the upstream by default' '
+	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 &&
+		test_commit three &&
+		git fetch &&
+		git history squash --no-edit &&
+		check_log_subjects HEAD <<-\EOF &&
+		one
+		base
+		EOF
+		test_cmp_rev three^{tree} HEAD^{tree}
+	)
 '
 
 test_expect_success 'errors on an empty range' '

base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
gitgitgadget
Patrick Steinhardt· Dec 1, 2026, 15:47 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH] history: squash the commits not in the upstream by default

On Wed, Nov 25, 2026 at 07:03:21PM +0000, Harald Nordgren via GitGitGadget wrote:
Show 11 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
> 
> "git history squash" with no arguments refuses to run and asks for a
> revision range, even though the commits to squash are usually the ones
> on the current branch that its upstream does not have.
> 
> The subcommand added in 6fd039c984 (history: add skeleton for squash
> subcommand, 2026-09-08) started out on the list as "git rebase --squash
> [<upstream>]", which folded "@{upstream}..HEAD" when given no argument.
> It moved to "git history" to take any range, and that default was lost
> in the move.

Yes, but isn't that a good thing? I wonder whether it becomes too magical when we start to assume the user's intention like this, and users may have a hard time to recover from such an operation. So honestly, I'm not sure that just doing something and hoping that this is what the user wants is sensible and I'd rather prefer to keep the status quo.

I am of course open to have my opinion changed in case there are good arguments for it.

Thanks!
Patrick

← back to recent threads