{"thread":{"id":"66932","subject":"[PATCH] history: squash the commits not in the upstream by default","startedAt":"2026-11-25T19:03:21Z","lastAt":"2026-12-01T15:47:43Z","messageCount":2,"participants":["Harald Nordgren via GitGitGadget","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559377","messageId":"pull.2463.git.git.1795633401382.gitgitgadget@gmail.com","threadId":"66932","inReplyTo":null,"subject":"[PATCH] history: squash the commits not in the upstream by default","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-25T19:03:21Z","receivedAt":"2026-11-25T19:03:21Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\n\"git history squash\" with no arguments refuses to run and asks for a\nrevision range, even though the commits to squash are usually the ones\non the current branch that its upstream does not have.\n\nThe subcommand added in 6fd039c984 (history: add skeleton for squash\nsubcommand, 2026-09-08) started out on the list as \"git rebase --squash\n[<upstream>]\", which folded \"@{upstream}..HEAD\" when given no argument.\nIt moved to \"git history\" to take any range, and that default was lost\nin the move.\n\nSquash the commits since the branch forked off its upstream, so this\nkeeps working after a fetch brings in new upstream commits.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    history: squash the commits not in the upstream by default\n    \n    Make git history squash with no range default to the commits on the\n    current branch that are not in its upstream, like git rebase does.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2463%2FHaraldNordgren%2Fhistory-squash-default-upstream-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2463/HaraldNordgren/history-squash-default-upstream-v1\nPull-Request: https://github.com/git/git/pull/2463\n\n Documentation/git-history.adoc |  8 ++++--\n builtin/history.c              | 49 ++++++++++++++++++++++++++--------\n t/t3455-history-squash.sh      | 31 +++++++++++++++++++--\n 3 files changed, 73 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 1fcc30150a..eaf75102d7 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -12,7 +12,7 @@ git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]\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>]\n \n DESCRIPTION\n -----------\n@@ -117,7 +117,7 @@ linkgit:gitglossary[7].\n It is invalid to select either all or no hunks, as that would lead to\n one of the commits becoming empty.\n \n-`squash <revision-range>`::\n+`squash [<revision-range>]`::\n \tFold all commits in _<revision-range>_ into the oldest commit of that\n \trange. The resulting commit keeps the oldest commit's authorship and\n \ttakes the tree of the range's newest commit, so the whole range\n@@ -133,6 +133,10 @@ given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is\n already on `topic`. Rev-list options may also be given, but any that would\n change how the range is walked are overridden with a warning.\n +\n+Without _<revision-range>_, all commits on the current branch that are not in\n+its upstream are squashed, starting from the merge base of `HEAD` and\n+`@{upstream}`.\n++\n An editor opens pre-filled with the messages of all the folded commits so you\n can combine them. With `--no-edit`, the oldest commit's message is preserved\n instead, except that an `amend!` commit targeting it replaces its message.\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 43ba76f5a1..0e5c5f2cc8 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -38,7 +38,7 @@\n #define GIT_HISTORY_SPLIT_USAGE \\\n \tN_(\"git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]\")\n #define GIT_HISTORY_SQUASH_USAGE \\\n-\tN_(\"git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] <revision-range>\")\n+\tN_(\"git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] [<revision-range>]\")\n \n static void change_data_free(void *util, const char *str UNUSED)\n {\n@@ -1319,7 +1319,7 @@ static int build_squash_message(struct repository *repo,\n \t\t\t\tstruct strbuf *out);\n \n static int setup_squash_revisions(struct repository *repo,\n-\t\t\t\t  int argc, const char **argv,\n+\t\t\t\t  struct strvec *args,\n \t\t\t\t  struct rev_info *revs)\n {\n \trepo_init_revisions(repo, revs, NULL);\n@@ -1331,9 +1331,9 @@ static int setup_squash_revisions(struct repository *repo,\n \trevs->limited = 1;\n \trevs->ancestry_path_implicit_bottoms = 1;\n \n-\targc = setup_revisions(argc, argv, revs, NULL);\n-\tif (argc > 1)\n-\t\treturn error(_(\"unrecognized argument: %s\"), argv[1]);\n+\tsetup_revisions_from_strvec(args, revs, NULL);\n+\tif (args->nr > 1)\n+\t\treturn error(_(\"unrecognized argument: %s\"), args->v[1]);\n \n \tif (revs->reverse != 1 || revs->topo_order != 1 ||\n \t    revs->sort_order != REV_SORT_IN_GRAPH_ORDER ||\n@@ -1372,7 +1372,7 @@ static int setup_squash_revisions(struct repository *repo,\n static int resolve_squash_range(struct repository *repo,\n \t\t\t\tbool update_branches,\n \t\t\t\tbool edit_message,\n-\t\t\t\tint argc, const char **argv,\n+\t\t\t\tstruct strvec *args,\n \t\t\t\tstruct commit **oldest_out,\n \t\t\t\tstruct commit **tip_out,\n \t\t\t\tchar **message_out)\n@@ -1387,7 +1387,7 @@ static int resolve_squash_range(struct repository *repo,\n \tstruct ref_array refs = { 0 };\n \n \tsubject_data.edit_message = edit_message;\n-\tret = setup_squash_revisions(repo, argc, argv, &revs);\n+\tret = setup_squash_revisions(repo, args, &revs);\n \tif (ret < 0)\n \t\tgoto out;\n \n@@ -1612,6 +1612,28 @@ out:\n \treturn ret;\n }\n \n+static int default_squash_range(struct repository *repo, struct strvec *args)\n+{\n+\tstruct commit *upstream, *head;\n+\tstruct commit_list *bases = NULL;\n+\n+\tupstream = lookup_commit_reference_by_name(\"@{upstream}\");\n+\tif (!upstream)\n+\t\treturn error(_(\"could not resolve the upstream of HEAD\"));\n+\thead = lookup_commit_reference_by_name(\"HEAD\");\n+\tif (!head)\n+\t\treturn error(_(\"could not resolve HEAD\"));\n+\tif (repo_get_merge_bases(repo, upstream, head, &bases) < 0)\n+\t\treturn -1;\n+\n+\tstrvec_push(args, \"squash\");\n+\tfor (struct commit_list *b = bases; b; b = b->next)\n+\t\tstrvec_pushf(args, \"^%s\", oid_to_hex(&b->item->object.oid));\n+\tstrvec_push(args, \"HEAD\");\n+\tcommit_list_free(bases);\n+\treturn 0;\n+}\n+\n static int cmd_history_squash(int argc,\n \t\t\t      const char **argv,\n \t\t\t      const char *prefix,\n@@ -1636,6 +1658,7 @@ static int cmd_history_squash(int argc,\n \t\tOPT_HISTORY_GPG_SIGN(&sign_commit),\n \t\tOPT_END(),\n \t};\n+\tstruct strvec args = STRVEC_INIT;\n \tstruct strbuf reflog_msg = STRBUF_INIT;\n \tstruct commit *oldest, *tip, *rewritten;\n \tconst struct object_id *base_tree_oid, *tip_tree_oid;\n@@ -1647,19 +1670,22 @@ static int cmd_history_squash(int argc,\n \targc = parse_options(argc, argv, prefix, options, usage,\n \t\t\t     PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0);\n \tif (argc < 2) {\n-\t\tret = error(_(\"command expects a revision range\"));\n-\t\tgoto out;\n+\t\tret = default_squash_range(repo, &args);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\t} else {\n+\t\tstrvec_pushv(&args, argv);\n \t}\n \n \tif (action == REF_ACTION_DEFAULT)\n \t\taction = REF_ACTION_BRANCHES;\n \n \tstrbuf_addstr(&reflog_msg, \"squash: updating \");\n-\tstrbuf_join_argv(&reflog_msg, argc - 1, argv + 1, ' ');\n+\tstrbuf_join_argv(&reflog_msg, args.nr - 1, args.v + 1, ' ');\n \n \tret = resolve_squash_range(repo, action == REF_ACTION_BRANCHES,\n \t\t\t\t   edit,\n-\t\t\t\t   argc, argv, &oldest, &tip,\n+\t\t\t\t   &args, &oldest, &tip,\n \t\t\t\t   &message_template);\n \tif (ret < 0)\n \t\tgoto out;\n@@ -1693,6 +1719,7 @@ static int cmd_history_squash(int argc,\n \tret = 0;\n \n out:\n+\tstrvec_clear(&args);\n \tstrbuf_release(&reflog_msg);\n \trelease_revisions(&revs);\n \tfree(message_template);\ndiff --git a/t/t3455-history-squash.sh b/t/t3455-history-squash.sh\nindex 237afdfa26..ac33d1cec3 100755\n--- a/t/t3455-history-squash.sh\n+++ b/t/t3455-history-squash.sh\n@@ -111,9 +111,36 @@ test_expect_success 'setup linear history touching two files' '\n \t\ttest_commit three file d\n '\n \n-test_expect_success 'errors on missing range argument' '\n+test_expect_success 'errors on missing range argument without an upstream' '\n \ttest_must_fail git history squash 2>err &&\n-\ttest_grep \"expects a revision range\" err\n+\ttest_grep \"no upstream configured\" err\n+'\n+\n+test_expect_success 'squashes the commits not in the upstream by default' '\n+\ttest_when_finished \"rm -rf upstream downstream\" &&\n+\tgit init upstream &&\n+\t(\n+\t\tcd upstream &&\n+\t\ttest_commit base\n+\t) &&\n+\tgit clone --single-branch upstream downstream &&\n+\t(\n+\t\tcd upstream &&\n+\t\ttest_commit upstream-moved\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit one &&\n+\t\ttest_commit two &&\n+\t\ttest_commit three &&\n+\t\tgit fetch &&\n+\t\tgit history squash --no-edit &&\n+\t\tcheck_log_subjects HEAD <<-\\EOF &&\n+\t\tone\n+\t\tbase\n+\t\tEOF\n+\t\ttest_cmp_rev three^{tree} HEAD^{tree}\n+\t)\n '\n \n test_expect_success 'errors on an empty range' '\n\nbase-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e\n-- \ngitgitgadget\n\n"},{"id":"559808","messageId":"aw7sH9-JCd43kCDX@pks.im","threadId":"66932","inReplyTo":"pull.2463.git.git.1795633401382.gitgitgadget@gmail.com","subject":"Re: [PATCH] history: squash the commits not in the upstream by default","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-12-01T15:47:43Z","receivedAt":"2026-12-01T15:47:43Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Nov 25, 2026 at 07:03:21PM +0000, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> \"git history squash\" with no arguments refuses to run and asks for a\n> revision range, even though the commits to squash are usually the ones\n> on the current branch that its upstream does not have.\n> \n> The subcommand added in 6fd039c984 (history: add skeleton for squash\n> subcommand, 2026-09-08) started out on the list as \"git rebase --squash\n> [<upstream>]\", which folded \"@{upstream}..HEAD\" when given no argument.\n> It moved to \"git history\" to take any range, and that default was lost\n> in the move.\n\nYes, but isn't that a good thing? I wonder whether it becomes too\nmagical when we start to assume the user's intention like this, and\nusers may have a hard time to recover from such an operation. So\nhonestly, I'm not sure that just doing something and hoping that this is\nwhat the user wants is sensible and I'd rather prefer to keep the status\nquo.\n\nI am of course open to have my opinion changed in case there are good\narguments for it.\n\nThanks!\n\nPatrick\n\n"}]}