{"thread":{"id":"66417","subject":"[PATCH] branch: let --delete-merged find squash merged branches","startedAt":"2026-09-29T07:30:33Z","lastAt":"2026-11-20T23:02:53Z","messageCount":104,"participants":["Harald Nordgren via GitGitGadget","Kristoffer Haugsbakk","Harald Nordgren","D. Ben Knoble","Phillip Wood","Junio C Hamano","Jasper Mayone","Tuomas Ahola"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"553554","messageId":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":null,"subject":"[PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T07:30:30Z","receivedAt":"2026-09-29T07:30:33Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nBranches merged on GitHub with \"Squash and merge\" or \"Rebase and\nmerge\" are never deleted by \"git branch --delete-merged\". The upstream\nholds a rewritten copy of their work, so their tips are not reachable\nfrom it and they look unmerged forever.\n\nTreat such a branch as merged when some upstream commit since the fork\npoint contains all of its changes, so that merging the branch into\nthat commit would change nothing. Name that commit in the output so\nthe user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first upstream commit that contains the changes is used, so the\nbranch is deleted even if upstream later reverted or reworked them.\nNothing is lost, since that commit keeps them in the upstream history.\nA branch whose changes only partly landed is kept.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    branch: let --delete-merged find squash merged branches\n    \n    Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\"\n    are never deleted by git branch --delete-merged, because their tips are\n    not reachable from the upstream. This treats such a branch as merged\n    when some upstream commit contains all of its changes, and names that\n    commit in the output:\n    \n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n    \n    \n    After the release of 2.56, I saw people liking the --delete-merged\n    feature, but asking for this. A lot of people, me included prefer\n    squash-merge and it currently doesn't work with --delete-merged.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v1\nPull-Request: https://github.com/git/git/pull/2425\n\n Documentation/git-branch.adoc |  15 +--\n builtin/branch.c              | 183 ++++++++++++++++++++++++++++++++--\n t/t3200-branch.sh             |  74 ++++++++++++++\n 3 files changed, 259 insertions(+), 13 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..0427324de1 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -204,12 +204,15 @@ This option is only applicable in non-verbose mode.\n \n `--delete-merged <pattern>`::\n \tDelete local branches whose configured upstream matches\n-\t_<pattern>_, but only when their tip is reachable from that\n-\tupstream. In other words, the work on the branch has already\n-\tlanded on the upstream it tracks, so the local copy is no longer\n-\tneeded. _<pattern>_ may name a ref, a remote (using the branch its\n-\t`HEAD` points at), or a shell-style glob. The option can be\n-\trepeated to widen the upstream match.\n+\t_<pattern>_, but only when their work has already landed on that\n+\tupstream, so the local copy is no longer needed. This is the case\n+\twhen their tip is reachable from the upstream, or when some\n+\tupstream commit contains all of their changes, as happens after\n+\ta squash or rebase merge, even if those changes were later\n+\treverted. The message for such a branch names the first upstream\n+\tcommit that contains its changes. _<pattern>_ may name a ref, a\n+\tremote (using the branch its `HEAD` points at), or a shell-style\n+\tglob. The option can be repeated to widen the upstream match.\n \tOptional _<branch-pattern>_ arguments limit which local branches\n \tare considered, e.g. `git branch --delete-merged 'origin/*'\n \t'topic-*'`.\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..982a8abe24 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,13 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"merge-ll.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -236,7 +243,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +341,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +366,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -824,6 +851,134 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id base_oid, branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+static void collect_branch_changes(struct commit *base, struct commit *rev,\n+\t\t\t\t   struct branch_change **changes,\n+\t\t\t\t   size_t *nr, size_t *alloc)\n+{\n+\tstruct diff_options opt;\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&opt);\n+\tdiff_tree_oid(get_commit_tree_oid(base), get_commit_tree_oid(rev),\n+\t\t      \"\", &opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(*changes, *nr + 1, *alloc);\n+\t\tchange = &(*changes)[(*nr)++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&opt);\n+}\n+\n+static int merge_keeps_upstream(const struct branch_change *change,\n+\t\t\t\tconst struct object_id *upstream_oid)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmbuffer_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\tif (oideq(&oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn 0;\n+\treturn merge_keeps_upstream(change, &oid);\n+}\n+\n+static struct commit *find_landed_commit(struct commit *rev,\n+\t\t\t\t\t struct commit *upstream)\n+{\n+\tstruct commit_list *merge_bases = NULL;\n+\tstruct branch_change *changes = NULL;\n+\tsize_t changes_nr = 0, changes_alloc = 0;\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tif (repo_get_merge_bases(the_repository, upstream, rev,\n+\t\t\t\t &merge_bases) < 0)\n+\t\texit(128);\n+\tif (!merge_bases)\n+\t\treturn NULL;\n+\tcollect_branch_changes(merge_bases->item, rev, &changes,\n+\t\t\t       &changes_nr, &changes_alloc);\n+\tcommit_list_free(merge_bases);\n+\tif (!changes_nr)\n+\t\treturn NULL;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&rev->object.oid));\n+\tstrvec_push(&args, \"--\");\n+\tfor (size_t i = 0; i < changes_nr; i++)\n+\t\tstrvec_pushf(&args, \":(literal)%s\", changes[i].path);\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs))) {\n+\t\tsize_t i;\n+\n+\t\tfor (i = 0; i < changes_nr; i++)\n+\t\t\tif (!change_landed(&changes[i], commit))\n+\t\t\t\tbreak;\n+\t\tif (i == changes_nr)\n+\t\t\tlanded = commit;\n+\t}\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\n+\tfor (size_t i = 0; i < changes_nr; i++)\n+\t\tfree(changes[i].path);\n+\tfree(changes);\n+\treturn landed;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -832,6 +987,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -852,6 +1008,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n+\t\tstruct commit *landed = NULL;\n \t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n@@ -867,8 +1024,17 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n-\t\t\tcontinue;\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n+\t\t\t\tupstream_refname);\n+\n+\t\t\tif (!rev || !upstream ||\n+\t\t\t    !(landed = find_landed_commit(rev, upstream)))\n+\t\t\t\tcontinue;\n+\t\t}\n \n \t\tstrbuf_reset(&key);\n \t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n@@ -882,6 +1048,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t}\n \n \t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (landed)\n+\t\t\tstrmap_put(&landed_commits, branch_name, landed);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n@@ -895,7 +1063,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -903,6 +1071,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1135,7 +1304,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..3b05718bab 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1979,6 +1979,80 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-merged deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial partial-landed partial-pending &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n-- \ngitgitgadget\n"},{"id":"553557","messageId":"9a6bfc3c-8759-4fbe-9e90-5dec9d00e278@app.fastmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-09-29T07:46:45Z","receivedAt":"2026-09-29T07:47:08Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Tue, Sep 29, 2026, at 09:30, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n> merge\" are never deleted by \"git branch --delete-merged\". The upstream\n> holds a rewritten copy of their work, so their tips are not reachable\n> from it and they look unmerged forever.\n\nAn example closer to git(1)’s home:\n\n    git merge --squash\n    git commit\n\n>[snip]\n"},{"id":"553560","messageId":"CAHwyqnVf_D3qV1OVYiCnLz2tVteRXdWYTGBaNTJpkVtDwCC1vg@mail.gmail.com","threadId":"66417","inReplyTo":"9a6bfc3c-8759-4fbe-9e90-5dec9d00e278@app.fastmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-29T07:52:01Z","receivedAt":"2026-09-29T07:52:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Tue, Sep 29, 2026 at 9:47 AM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Tue, Sep 29, 2026, at 09:30, Harald Nordgren via GitGitGadget wrote:\n> > From: Harald Nordgren <haraldnordgren@gmail.com>\n> >\n> > Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n> > merge\" are never deleted by \"git branch --delete-merged\". The upstream\n> > holds a rewritten copy of their work, so their tips are not reachable\n> > from it and they look unmerged forever.\n>\n> An example closer to git(1)’s home:\n>\n>     git merge --squash\n>     git commit\n\nTrue. But likely it opens up the question of _why_ would anyone on\nupstream be doing such destructive actions? Well, then the answer is\nof course that millions of users (including) me do that via GitHub all\nthe time.\n\nMaybe I should include both examples in my text.\n\n\nHarald\n"},{"id":"553561","messageId":"d4fd92ea-b1c5-4528-9e9e-0b1ab600891e@app.fastmail.com","threadId":"66417","inReplyTo":"CAHwyqnVf_D3qV1OVYiCnLz2tVteRXdWYTGBaNTJpkVtDwCC1vg@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-09-29T08:08:54Z","receivedAt":"2026-09-29T08:09:20Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Tue, Sep 29, 2026, at 09:52, Harald Nordgren wrote:\n> On Tue, Sep 29, 2026 at 9:47 AM Kristoffer Haugsbakk\n> <kristofferhaugsbakk@fastmail.com> wrote:\n>>\n>> On Tue, Sep 29, 2026, at 09:30, Harald Nordgren via GitGitGadget wrote:\n>> > From: Harald Nordgren <haraldnordgren@gmail.com>\n>> >\n>> > Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n>> > merge\" are never deleted by \"git branch --delete-merged\". The upstream\n>> > holds a rewritten copy of their work, so their tips are not reachable\n>> > from it and they look unmerged forever.\n>>\n>> An example closer to git(1)’s home:\n>>\n>>     git merge --squash\n>>     git commit\n>\n> True. But likely it opens up the question of _why_ would anyone on\n> upstream be doing such destructive actions? Well, then the answer is\n> of course that millions of users (including) me do that via GitHub all\n> the time.\n>\n> Maybe I should include both examples in my text.\n\nMy *guess* is that `git merge --squash` inspired the forge squashes.\nBut the forges popularized it.\n\nThe apparent `git merge --squash` approach of using `git log` for\nconcatenating the commit messages isn’t that nice in my opinion. So I\nwonder how much it is used.\n"},{"id":"553582","messageId":"CALnO6CBq5Udc1rbk6efRj1q5pJNUDt-uvmSGDDen=CMH0dFOHQ@mail.gmail.com","threadId":"66417","inReplyTo":"d4fd92ea-b1c5-4528-9e9e-0b1ab600891e@app.fastmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-09-29T11:18:43Z","receivedAt":"2026-09-29T11:18:54Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Sep 29, 2026 at 4:17 AM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Tue, Sep 29, 2026, at 09:52, Harald Nordgren wrote:\n> > On Tue, Sep 29, 2026 at 9:47 AM Kristoffer Haugsbakk\n> > <kristofferhaugsbakk@fastmail.com> wrote:\n> >>\n> >> On Tue, Sep 29, 2026, at 09:30, Harald Nordgren via GitGitGadget wrote:\n> >> > From: Harald Nordgren <haraldnordgren@gmail.com>\n> >> >\n> >> > Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n> >> > merge\" are never deleted by \"git branch --delete-merged\". The upstream\n> >> > holds a rewritten copy of their work, so their tips are not reachable\n> >> > from it and they look unmerged forever.\n> >>\n> >> An example closer to git(1)’s home:\n> >>\n> >>     git merge --squash\n> >>     git commit\n> >\n> > True. But likely it opens up the question of _why_ would anyone on\n> > upstream be doing such destructive actions? Well, then the answer is\n> > of course that millions of users (including) me do that via GitHub all\n> > the time.\n> >\n> > Maybe I should include both examples in my text.\n>\n> My *guess* is that `git merge --squash` inspired the forge squashes.\n> But the forges popularized it.\n>\n> The apparent `git merge --squash` approach of using `git log` for\n> concatenating the commit messages isn’t that nice in my opinion. So I\n> wonder how much it is used.\n\nThe same behavior is present in GitHub's default squash merge message,\nand almost no one I work with bothers to edit it.\n\nIt's really sad to lose the opportunity to have good commit messages\nwhen using squash-and-merge on a forge---not because we *cannot*, but\nbecause the defaults do not *encourage* it (and we all know how\ndefaults affect user behavior!).\n\nAnyway, see https://benknoble.github.io/blog/2024/08/02/github-squash/\nfor a distillation of my thoughts from working around folks that\nsquash carelessly.\n\nSo, anecdotally: it is used widely due to defaults. Blech.\n\n-- \nD. Ben Knoble\n"},{"id":"553588","messageId":"CALnO6CBwWy3aafyDJPKFk5vuWy2EF1n1Oc=W7+RVAE3rxpXwiw@mail.gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-09-29T11:26:20Z","receivedAt":"2026-09-29T11:26:33Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"Without looking too much further…\n\nOn Tue, Sep 29, 2026 at 3:33 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n> merge\" are never deleted by \"git branch --delete-merged\". The upstream\n> holds a rewritten copy of their work, so their tips are not reachable\n> from it and they look unmerged forever.\n>\n> Treat such a branch as merged when some upstream commit since the fork\n> point contains all of its changes, so that merging the branch into\n> that commit would change nothing. Name that commit in the output so\n> the user can see where the work went:\n>\n>     Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n>\n> The first upstream commit that contains the changes is used, so the\n> branch is deleted even if upstream later reverted or reworked them.\n> Nothing is lost, since that commit keeps them in the upstream history.\n> A branch whose changes only partly landed is kept.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n\n…in the rebase case, I would expect something like git-log's\n--cherry-mark option (or really the algorithm behind it, git-cherry,\nand git-range-diff) to be useful for identifying rebased branches. But\nof course even rebase-merged branches can end up with minor\ndifferences (say, a commit was made upstream before that branch was\nrebased with an identical change; no conflict occurs, but the new\ncommit differs from the old by not having that change).\n\nIn the squash case, I suppose the best we can do is check that all our\nchanges were applied at some point between the merge-base and the tip.\nThere probably won't be any tree-same commits, though maybe a\n(premature?) optimization can return early if the trees match exactly.\n\nIt looked like you don't distinguish the 2 cases in the code, and I\nthink that's reasonable: we wouldn't know a priori whether to check\nfor a rebased series or a squashed commit, so we'd have to run both\nchecks, and the latter presumably subsumes the former.\n\nAnyway, I can see how this would all be fairly expensive---on one repo\nI work in, git-range-diff can be somewhat slow depending on how many\ncommits are in the range, I think. I don't know if it's worth trying\nto state that for folks, though? If we ever make improvements to\nperformance, we'd have to remember to remove the \"this may be slow\"\ntext.\n\n> After the release of 2.56, I saw people liking the --delete-merged\n>    feature, but asking for this. A lot of people, me included prefer\n>    squash-merge and it currently doesn't work with --delete-merged.\n\nBtw, I wonder if you can share where you saw this? 2.56 was released\nso recently I'm (pleasantly) surprised there's already feedback on\nthis!\n\n-- \nD. Ben Knoble\n"},{"id":"553601","messageId":"4c4fba69-e474-4ee3-8e34-73e76d42d3d5@app.fastmail.com","threadId":"66417","inReplyTo":"CALnO6CBq5Udc1rbk6efRj1q5pJNUDt-uvmSGDDen=CMH0dFOHQ@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-09-29T13:31:38Z","receivedAt":"2026-09-29T13:32:03Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Tue, Sep 29, 2026, at 13:18, D. Ben Knoble wrote:\n> On Tue, Sep 29, 2026 at 4:17 AM Kristoffer Haugsbakk\n> <kristofferhaugsbakk@fastmail.com> wrote:\n>>\n>> On Tue, Sep 29, 2026, at 09:52, Harald Nordgren wrote:\n>> > [snip]\n>> >\n>> > Maybe I should include both examples in my text.\n>>\n>> My *guess* is that `git merge --squash` inspired the forge squashes.\n>> But the forges popularized it.\n>>\n>> The apparent `git merge --squash` approach of using `git log` for\n>> concatenating the commit messages isn’t that nice in my opinion. So I\n>> wonder how much it is used.\n>\n> The same behavior is present in GitHub's default squash merge message,\n> and almost no one I work with bothers to edit it.\n\nThe asterisk bullet points on GitHub are better than `git merge\n--squash`:\n\n    Squashed commit of the following:\n\n    [just `git log` of the commits in the range]\n\n> It's really sad to lose the opportunity to have good commit messages\n> when using squash-and-merge on a forge---not because we *cannot*, but\n> because the defaults do not *encourage* it (and we all know how\n> defaults affect user behavior!).\n\nIn my opinion squash merges cannot be implemented in a good way, in a\nway that leads to good commits. Fundamentally not. It’s the button to\nboth squash “oops” and the incremental, valuable commits, resulting in a\nblob where even a manually written commit message cannot document all\nthe changes properly. And the reasons why are laid out in your article,\nI think...\n\n>\n> Anyway, see https://benknoble.github.io/blog/2024/08/02/github-squash/\n> for a distillation of my thoughts from working around folks that\n> squash carelessly.\n>\n> So, anecdotally: it is used widely due to defaults. Blech.\n\nWhich is excellent. Thanks for writing it.\n"},{"id":"553603","messageId":"CAHwyqnUkz+7FH4QY-EB__dOc2rWNNwy9EL_b7R7cA1oPvequ=A@mail.gmail.com","threadId":"66417","inReplyTo":"CALnO6CBwWy3aafyDJPKFk5vuWy2EF1n1Oc=W7+RVAE3rxpXwiw@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-29T13:44:07Z","receivedAt":"2026-09-29T13:44:47Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > After the release of 2.56, I saw people liking the --delete-merged\n> >    feature, but asking for this. A lot of people, me included prefer\n> >    squash-merge and it currently doesn't work with --delete-merged.\n>\n> Btw, I wonder if you can share where you saw this? 2.56 was released\n> so recently I'm (pleasantly) surprised there's already feedback on\n> this!\n\nReddit thread: https://www.reddit.com/r/git/comments/1wsnrl9/comment/pcncf5t\n\nThere was only one person asking to clean up squashed branches. But I\nalso started thinking about it the other day, when 2.56 drew closer,\nand I realized that friends that work in companies using squash merge\nwon't get any benefit from this.\n\nBlog posts that is drawing attention to this new feature (not\nnecessary feedbacking on it):\n\n- https://github.blog/open-source/git/highlights-from-git-2-56/\n- https://about.gitlab.com/blog/whats-new-in-git-2-56-0/\n- https://9to5linux.com/git-2-56-adds-new-options-for-cleaning-up-branches-and-resolving-conflicts\n- https://linuxiac.com/git-2-56-released-with-safer-conflict-resolution-and-performance-gains/\n\nBtw, I love squash merge!\n\n\nHarald\n"},{"id":"553604","messageId":"CAHwyqnXLAQgTen2nu6xX63ch7Z9V8kZ-c3zbZgw_xwEVYaz=oA@mail.gmail.com","threadId":"66417","inReplyTo":"CALnO6CBwWy3aafyDJPKFk5vuWy2EF1n1Oc=W7+RVAE3rxpXwiw@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-29T13:52:53Z","receivedAt":"2026-09-29T13:53:31Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> Anyway, I can see how this would all be fairly expensive---on one repo\n> I work in, git-range-diff can be somewhat slow depending on how many\n> commits are in the range, I think. I don't know if it's worth trying\n> to state that for folks, though? If we ever make improvements to\n> performance, we'd have to remember to remove the \"this may be slow\"\n> text.\n\nIt was much slower in my first iterations, so running this on my local\nGit repo now does not feel painfully slow. Although slower than\nwithout this feature.\n\nWe might hide it behind a feature flag?\n\n\nHarald\n"},{"id":"553607","messageId":"CALnO6CC4fS0LWe6ta7B-C3dQXSjfYhab-e=EHdneRRqfa9mAZw@mail.gmail.com","threadId":"66417","inReplyTo":"CAHwyqnXLAQgTen2nu6xX63ch7Z9V8kZ-c3zbZgw_xwEVYaz=oA@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-09-29T15:28:08Z","receivedAt":"2026-09-29T15:28:23Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Sep 29, 2026 at 9:53 AM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> > Anyway, I can see how this would all be fairly expensive---on one repo\n> > I work in, git-range-diff can be somewhat slow depending on how many\n> > commits are in the range, I think. I don't know if it's worth trying\n> > to state that for folks, though? If we ever make improvements to\n> > performance, we'd have to remember to remove the \"this may be slow\"\n> > text.\n>\n> It was much slower in my first iterations, so running this on my local\n> Git repo now does not feel painfully slow. Although slower than\n> without this feature.\n>\n> We might hide it behind a feature flag?\n\nI could imagine wanting a CSV-style value for the option, like\n\"--delete-merged=squashed,rebased\" vs. \"--delete-merged=merged\"\n(current default), or something. But I'd have to think about whether\nI'm suggesting that only to work around performance or to also support\nactual use cases. Mostly I think people just want to go \"gah, delete\nmerged branches\" and not think about it further… hm.\n\n-- \nD. Ben Knoble\n"},{"id":"553627","messageId":"CAHwyqnXA6kxktxNRPCbq0hTdH+G+3jSkCq5ghkS6QkJEgA1wkw@mail.gmail.com","threadId":"66417","inReplyTo":"CALnO6CC4fS0LWe6ta7B-C3dQXSjfYhab-e=EHdneRRqfa9mAZw@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-29T18:34:23Z","receivedAt":"2026-09-29T18:35:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > We might hide it behind a feature flag?\n>\n> I could imagine wanting a CSV-style value for the option, like\n> \"--delete-merged=squashed,rebased\" vs. \"--delete-merged=merged\"\n> (current default), or something. But I'd have to think about whether\n> I'm suggesting that only to work around performance or to also support\n> actual use cases. Mostly I think people just want to go \"gah, delete\n> merged branches\" and not think about it further… hm.\n\nFor me, the only reason to hide this would be for performance.\n\nYes, I think most people (me included) would rather have the simplest\npossible command. I'd rather not have to specify the pattern so that\nthese are to the same:\n\n    git branch --delete-merged\n    git branch --delete-merged '*/*'\n\n\nHarald\n"},{"id":"554101","messageId":"39a28064-1698-4971-a80f-4a4c4dcdd8d9@gmail.com","threadId":"66417","inReplyTo":"CALnO6CBwWy3aafyDJPKFk5vuWy2EF1n1Oc=W7+RVAE3rxpXwiw@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-04T09:54:10Z","receivedAt":"2026-10-04T09:54:14Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 29/09/2026 12:26, D. Ben Knoble wrote:\n> On Tue, Sep 29, 2026 at 3:33 AM Harald Nordgren via GitGitGadget\n> <gitgitgadget@gmail.com> wrote:\n> \n> …in the rebase case, I would expect something like git-log's\n> --cherry-mark option (or really the algorithm behind it, git-cherry,\n> and git-range-diff) \n\nThat's what I was expecting as well. It would be worth carefully \nstudying the implementation of git-cherry. \"git cherry A...B\" \nprecalculates the patch-ids from the side of the merge base that has the \nfewest commits and then walks the other side to compare them. While it \nis walking the other side I think it also looks at which paths were \nchanged to avoid calculating the patch-id for commits that cannot match. \nIt also batches fetches the blobs it needs in partial clones.\n\nAs far as I can see the implementation here makes a separate upstream \nrevision walk for each branch, and recalculates the upstream diffs each \ntime which seems less efficient than it could be.\n> to be useful for identifying rebased branches. But\n> of course even rebase-merged branches can end up with minor\n> differences (say, a commit was made upstream before that branch was\n> rebased with an identical change; no conflict occurs, but the new\n> commit differs from the old by not having that change).\n\nYes if a branch has been rebased before it is merged it may be altered \nsuch that we cannot detect it.\n> In the squash case, I suppose the best we can do is check that all our\n> changes were applied at some point between the merge-base and the tip.\n> There probably won't be any tree-same commits, though maybe a\n> (premature?) optimization can return early if the trees match exactly.\n\nIf we have\n\n(topic)  D - C - B - A\n                       \\\n  (main)    M - Q - P - O -\n             \\          /\n               - - S - -\n\nwhere M is a squashed merge of topic I think we have\n\n     M^2^{tree} == topic^{tree}\n     Merge-base(M^1, M^2) == Merge-base(topic, topic@{upstream})\n     $(git rev-list --count --right-only M^1...M^2) == 1\n\nIf you know your repository only has squash merges that were not rebased \nit would be a lot more efficient to just look at the trees and \nmerge-bases, especially in a blobless clone. Having an option to turn \noff the patch-id based detection would probably be useful in that case.\n\nI think detecting branches that have been squashed and/or rebased is a \nuseful improvement, but it needs careful implementation to be efficient \nenough that it is practical in large repositories and I'm unlikely to \nhave time to closely review it.\n\nThanks\n\nPhillip\n\n> It looked like you don't distinguish the 2 cases in the code, and I\n> think that's reasonable: we wouldn't know a priori whether to check\n> for a rebased series or a squashed commit, so we'd have to run both\n> checks, and the latter presumably subsumes the former.\n> \n> Anyway, I can see how this would all be fairly expensive---on one repo\n> I work in, git-range-diff can be somewhat slow depending on how many\n> commits are in the range, I think. I don't know if it's worth trying\n> to state that for folks, though? If we ever make improvements to\n> performance, we'd have to remember to remove the \"this may be slow\"\n> text.\n> \n>> After the release of 2.56, I saw people liking the --delete-merged\n>>     feature, but asking for this. A lot of people, me included prefer\n>>     squash-merge and it currently doesn't work with --delete-merged.\n> \n> Btw, I wonder if you can share where you saw this? 2.56 was released\n> so recently I'm (pleasantly) surprised there's already feedback on\n> this!\n> \n\n"},{"id":"554131","messageId":"CAHwyqnVoMnO_fYGJ0N29bQv=Lh5naZ0jc5uSpiS2urQMZVG5-Q@mail.gmail.com","threadId":"66417","inReplyTo":"39a28064-1698-4971-a80f-4a4c4dcdd8d9@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-04T22:28:36Z","receivedAt":"2026-10-04T22:29:15Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> As far as I can see the implementation here makes a separate upstream\n> revision walk for each branch, and recalculates the upstream diffs each\n> time which seems less efficient than it could be.\n\nYes, that can be improved!\n\n> > In the squash case, I suppose the best we can do is check that all our\n> > changes were applied at some point between the merge-base and the tip.\n> > There probably won't be any tree-same commits, though maybe a\n> > (premature?) optimization can return early if the trees match exactly.\n...\n> If you know your repository only has squash merges that were not rebased\n> it would be a lot more efficient to just look at the trees and\n> merge-bases, especially in a blobless clone. Having an option to turn\n> off the patch-id based detection would probably be useful in that case.\n\nMaybe yes, but for users I imagine they want the interface to be as\nsimple as possible.\n\nI'm iterating on the code on my side (sharing logic between branches,\netc) and it became fast on my local Git repo. If there are no\nperformance concerns then would we still want the option to turn it\noff?\n\n\nHarald\n"},{"id":"554508","messageId":"61ae371a-225c-4400-b878-8547547d1269@gmail.com","threadId":"66417","inReplyTo":"CAHwyqnVoMnO_fYGJ0N29bQv=Lh5naZ0jc5uSpiS2urQMZVG5-Q@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-08T15:25:39Z","receivedAt":"2026-10-08T15:25:39Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 04/10/2026 23:28, Harald Nordgren wrote:\n> ...\n>> If you know your repository only has squash merges that were not rebased\n>> it would be a lot more efficient to just look at the trees and\n>> merge-bases, especially in a blobless clone. Having an option to turn\n>> off the patch-id based detection would probably be useful in that case.\n> > Maybe yes, but for users I imagine they want the interface to be as\n> simple as possible.\n\nAgreed, they also don't want to wait to find squashed merges they know they do not have.\n\n> I'm iterating on the code on my side (sharing logic between branches,\n> etc) and it became fast on my local Git repo. If there are no\n> performance concerns then would we still want the option to turn it\n> off?\n\nEven an efficient implementation is going to be a lot slower when it is trying to find branches that have been squashed, so I think we probably do want a way to turn it off. That's especially true in partial clones where we'll have to download a bunch of blobs to do the squash detection. So long as it isn't diabolically slow enabling it by default is probably fine.\n\nThanks\n\nPhillip\n\n\n"},{"id":"554517","messageId":"CALnO6CB2qPtv6Cr4LL1x=ZPmD_zLw1GzLUKgXE-NcTWObbiLkA@mail.gmail.com","threadId":"66417","inReplyTo":"39a28064-1698-4971-a80f-4a4c4dcdd8d9@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-08T17:51:47Z","receivedAt":"2026-10-08T17:51:47Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Sun, Oct 4, 2026 at 5:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> On 29/09/2026 12:26, D. Ben Knoble wrote:\n> > On Tue, Sep 29, 2026 at 3:33 AM Harald Nordgren via GitGitGadget\n> > <gitgitgadget@gmail.com> wrote:\n> >\n> > …in the rebase case, I would expect something like git-log's\n> > --cherry-mark option (or really the algorithm behind it, git-cherry,\n> > and git-range-diff)\n>\n> That's what I was expecting as well. It would be worth carefully\n> studying the implementation of git-cherry. \"git cherry A...B\"\n> precalculates the patch-ids from the side of the merge base that has the\n> fewest commits and then walks the other side to compare them. While it\n> is walking the other side I think it also looks at which paths were\n> changed to avoid calculating the patch-id for commits that cannot match.\n> It also batches fetches the blobs it needs in partial clones.\n>\n> As far as I can see the implementation here makes a separate upstream\n> revision walk for each branch, and recalculates the upstream diffs each\n> time which seems less efficient than it could be.\n\nThanks for spelling that out!\n\n> > to be useful for identifying rebased branches. But\n> > of course even rebase-merged branches can end up with minor\n> > differences (say, a commit was made upstream before that branch was\n> > rebased with an identical change; no conflict occurs, but the new\n> > commit differs from the old by not having that change).\n>\n> Yes if a branch has been rebased before it is merged it may be altered\n> such that we cannot detect it.\n\nYep. I'm not sure what Harald (or we) would want to do here.\ngit-range-diff has trouble detecting these scenarios today, so maybe\nmatching that and later finding a way to improve is ok.\n\n> > In the squash case, I suppose the best we can do is check that all our\n> > changes were applied at some point between the merge-base and the tip.\n> > There probably won't be any tree-same commits, though maybe a\n> > (premature?) optimization can return early if the trees match exactly.\n>\n> If we have\n>\n> (topic)  D - C - B - A\n>                        \\\n>   (main)    M - Q - P - O -\n>              \\          /\n>                - - S - -\n>\n> where M is a squashed merge of topic I think we have\n>\n>      M^2^{tree} == topic^{tree}\n>      Merge-base(M^1, M^2) == Merge-base(topic, topic@{upstream})\n>      $(git rev-list --count --right-only M^1...M^2) == 1\n\nPerhaps we are thinking of 2 different things? In practice when I see\na merge created using GitHub's squash and merge option (and, I think,\nalso when using `merge --squash`), there is no second parent. You\ncould instead just get\n\n    (main) S - Q - P - O\n\nwhere S is A+B+C+D applied to Q (i.e., closer to a cherry-pick with\n--no-commit).\n\nAnd we know that S is not necessarily tree-same to topic's D, so…?\n\n-- \nD. Ben Knoble\n\n"},{"id":"554522","messageId":"CAHwyqnWkTvicU+U99j0MzzUUXeVnUj=FJJwUDR1F7DGk1hmtrA@mail.gmail.com","threadId":"66417","inReplyTo":"61ae371a-225c-4400-b878-8547547d1269@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-08T18:42:08Z","receivedAt":"2026-10-08T18:42:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> Even an efficient implementation is going to be a lot slower when it is\n> trying to find branches that have been squashed, so I think we probably\n> do want a way to turn it off. That's especially true in partial clones\n> where we'll have to download a bunch of blobs to do the squash\n> detection. So long as it isn't diabolically slow enabling it by default\n> is probably fine.\n\nA bit slower (depends on how much!) could be worth it for improved\nusability. This is not a command that users will run multiple times a\nday.\n\nI would hope we could have it on by default and add a flag to turn it\noff instead.\n\nFor partial or shallow clones, we should probably warn the user that\ndetection won't be able to search the full history, and offer them to\nunshallow, etc. It's very reasonable that some parts are turned off,\nwhen the user doesn't want to hold the complete repo.\n\n\n\nHarald\n\n"},{"id":"554558","messageId":"cf3bb2a4-5956-4ba5-9957-b0598f22a122@app.fastmail.com","threadId":"66417","inReplyTo":"CAHwyqnWkTvicU+U99j0MzzUUXeVnUj=FJJwUDR1F7DGk1hmtrA@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-09T08:16:21Z","receivedAt":"2026-10-09T08:16:21Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Thu, Oct 8, 2026, at 20:42, Harald Nordgren wrote:\n>> Even an efficient implementation is going to be a lot slower when it is\n>> trying to find branches that have been squashed, so I think we probably\n>> do want a way to turn it off. That's especially true in partial clones\n>> where we'll have to download a bunch of blobs to do the squash\n>> detection. So long as it isn't diabolically slow enabling it by default\n>> is probably fine.\n>\n> A bit slower (depends on how much!) could be worth it for improved\n> usability. This is not a command that users will run multiple times a\n> day.\n\nI would really like a solid delete operation that I can run, say,\novernight. I have better hygiene practices today but I didn’t back then,\nso things have piled up.\n\nOne big cleanup is worth a nightly run. So I don’t care about how slow\nit is.\n\n>[snip]\n\n"},{"id":"554571","messageId":"CAHwyqnWpWbeknZhaOzWfAmvQVQkDmKKmVYV-p_Zu-z7t3FA5+g@mail.gmail.com","threadId":"66417","inReplyTo":"cf3bb2a4-5956-4ba5-9957-b0598f22a122@app.fastmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-09T11:11:30Z","receivedAt":"2026-10-09T11:11:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Fri, Oct 9, 2026 at 10:16 AM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Thu, Oct 8, 2026, at 20:42, Harald Nordgren wrote:\n> >> Even an efficient implementation is going to be a lot slower when it is\n> >> trying to find branches that have been squashed, so I think we probably\n> >> do want a way to turn it off. That's especially true in partial clones\n> >> where we'll have to download a bunch of blobs to do the squash\n> >> detection. So long as it isn't diabolically slow enabling it by default\n> >> is probably fine.\n> >\n> > A bit slower (depends on how much!) could be worth it for improved\n> > usability. This is not a command that users will run multiple times a\n> > day.\n>\n> I would really like a solid delete operation that I can run, say,\n> overnight. I have better hygiene practices today but I didn’t back then,\n> so things have piled up.\n>\n> One big cleanup is worth a nightly run. So I don’t care about how slow\n> it is.\n\nI would love to have a `full-clean` command that bundles\n`deleted-merged` and all `prune` commands, and uses sensible defaults\nfor each one. (I have a hard time believing that it would need to run\novernight anyway).\n\nI have known people who just delete the whole repo on any signs of\ntrouble. People out there are so scared of Git, so let's give them\nsensible defaults to work with.\n\n\nHarald\n\n"},{"id":"554572","messageId":"CAHwyqnWmXM0fEn2X9p7g3fpzmXhfAJAqAj9SG0=6kOpunqfVUg@mail.gmail.com","threadId":"66417","inReplyTo":"CAHwyqnWkTvicU+U99j0MzzUUXeVnUj=FJJwUDR1F7DGk1hmtrA@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-09T11:18:30Z","receivedAt":"2026-10-09T11:18:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Interesting to note is that merged branches inside our repo change\nwhen Junio signs off on them. So without this functionality they are\nnot cleaned either.\n\n\nHarald\n\n"},{"id":"554599","messageId":"da5d6d28-b64b-4cfa-9998-93c69720b3c6@gmail.com","threadId":"66417","inReplyTo":"CALnO6CB2qPtv6Cr4LL1x=ZPmD_zLw1GzLUKgXE-NcTWObbiLkA@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-09T13:34:28Z","receivedAt":"2026-10-09T13:34:28Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Ben\n\nOn 08/10/2026 18:51, D. Ben Knoble wrote:\n> On Sun, Oct 4, 2026 at 5:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>>\n>> If we have\n>>\n>> (topic)  D - C - B - A\n>>                         \\\n>>    (main)    M - Q - P - O -\n>>               \\          /\n>>                 - - S - -\n>>\n>> where M is a squashed merge of topic I think we have\n>>\n>>       M^2^{tree} == topic^{tree}\n>>       Merge-base(M^1, M^2) == Merge-base(topic, topic@{upstream})\n>>       $(git rev-list --count --right-only M^1...M^2) == 1\n> > Perhaps we are thinking of 2 different things? In practice when I see\n> a merge created using GitHub's squash and merge option (and, I think,\n> also when using `merge --squash`), there is no second parent. You\n> could instead just get\n> >      (main) S - Q - P - O\n> > where S is A+B+C+D applied to Q (i.e., closer to a cherry-pick with\n> --no-commit).\n\nOh, you're right, I'd misremembered how \"merge --squash\" worked.\n\n> And we know that S is not necessarily tree-same to topic's D, so…?\n\nI agree we need to use patch ids rather than trees in that case.\n\nThanks\n\nPhillip\n\n\n"},{"id":"554600","messageId":"500a1b40-b877-4970-b279-f0a6fb42f810@gmail.com","threadId":"66417","inReplyTo":"CAHwyqnWkTvicU+U99j0MzzUUXeVnUj=FJJwUDR1F7DGk1hmtrA@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-09T13:37:36Z","receivedAt":"2026-10-09T13:37:36Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 08/10/2026 19:42, Harald Nordgren wrote:\n>> Even an efficient implementation is going to be a lot slower when it is\n>> trying to find branches that have been squashed, so I think we probably\n>> do want a way to turn it off. That's especially true in partial clones\n>> where we'll have to download a bunch of blobs to do the squash\n>> detection. So long as it isn't diabolically slow enabling it by default\n>> is probably fine.\n> > A bit slower (depends on how much!) could be worth it for improved\n> usability. This is not a command that users will run multiple times a\n> day.\n> > I would hope we could have it on by default and add a flag to turn it\n> off instead.\n\nThat's my hope too\n\n> For partial or shallow clones, we should probably warn the user that\n> detection won't be able to search the full history, and offer them to\n> unshallow, etc. It's very reasonable that some parts are turned off,\n> when the user doesn't want to hold the complete repo.\nI've not really though much about shallow clones, but for partial clones I think it is perfectly reasonable to go and fetch the blobs we need to calculate the patch ids and warn in the documentation that it can be slow.\n\nThanks\n\nPhillip\n\n"},{"id":"554602","messageId":"8f0ec8ae-5622-4d07-a4cc-753cf7e64982@gmail.com","threadId":"66417","inReplyTo":"CAHwyqnWmXM0fEn2X9p7g3fpzmXhfAJAqAj9SG0=6kOpunqfVUg@mail.gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-09T13:40:20Z","receivedAt":"2026-10-09T13:40:20Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 09/10/2026 12:18, Harald Nordgren wrote:\n> Interesting to note is that merged branches inside our repo change\n> when Junio signs off on them. So without this functionality they are\n> not cleaned either.\nThat's true for a patched based workflow like the one on this list whether or not the maintainer signs off the commits. It is slightly different from the squash merge case because you need to look for the individual commits upstream, rather than the combined changes from the branch.\n\nThanks\n\nPhillip\n\n"},{"id":"554617","messageId":"CAHwyqnWEXd4e+wA_tVoQPmmY2ZoH+W7u65QQ2Y=PTMGUWuGvZQ@mail.gmail.com","threadId":"66417","inReplyTo":"500a1b40-b877-4970-b279-f0a6fb42f810@gmail.com","subject":"Re: [PATCH] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-09T15:27:08Z","receivedAt":"2026-10-09T15:27:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> I've not really though much about shallow clones, but for partial clones\n> I think it is perfectly reasonable to go and fetch the blobs we need to\n> calculate the patch ids and warn in the documentation that it can be slow.\n\nWould you say the blobless case should be handled now, or can be left\nas a follow-up topic?\n\n\nHarald\n\n"},{"id":"554710","messageId":"pull.2425.v2.git.git.1791711665.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-11T09:41:03Z","receivedAt":"2026-10-11T09:41:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (2):\n  branch: let --delete-merged find squash merged branches\n  branch: add --no-squashed to skip the squash search\n\n Documentation/git-branch.adoc |  23 ++-\n builtin/branch.c              | 342 ++++++++++++++++++++++++++++++++--\n t/t3200-branch.sh             | 170 +++++++++++++++++\n 3 files changed, 509 insertions(+), 26 deletions(-)\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v2\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v1:\n\n 1:  c9ab56d8b2 ! 1:  94a6d23f8b branch: let --delete-merged find squash merged branches\n     @@ Metadata\n       ## Commit message ##\n          branch: let --delete-merged find squash merged branches\n      \n     -    Branches merged on GitHub with \"Squash and merge\" or \"Rebase and\n     -    merge\" are never deleted by \"git branch --delete-merged\". The upstream\n     -    holds a rewritten copy of their work, so their tips are not reachable\n     -    from it and they look unmerged forever.\n     +    A branch that upstream integrated with\n     +\n     +        git merge --squash topic\n     +        git commit\n     +\n     +    is never deleted by \"git branch --delete-merged\". The same goes for a\n     +    branch that was rebased or cherry-picked onto upstream. The upstream\n     +    holds a rewritten copy of its work, so its tip is not reachable from\n     +    it and it looks unmerged forever.\n     +\n     +    This happens all the time on hosting services. The \"Squash and merge\"\n     +    and \"Rebase and merge\" buttons on GitHub land pull requests this way,\n     +    so contributors to such projects collect local branches that\n     +    \"--delete-merged\" never cleans up.\n      \n          Treat such a branch as merged when some upstream commit since the fork\n          point contains all of its changes, so that merging the branch into\n     @@ builtin/branch.c\n       #include \"help.h\"\n       #include \"advice.h\"\n       #include \"commit-reach.h\"\n     ++#include \"commit-slab.h\"\n      +#include \"diff.h\"\n      +#include \"diffcore.h\"\n      +#include \"hex.h\"\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n       \treturn ret;\n       }\n       \n     ++enum change_state {\n     ++\tCHANGE_MISSING,\n     ++\tCHANGE_LANDED,\n     ++\tCHANGE_NEEDS_MERGE,\n     ++};\n     ++\n      +struct branch_change {\n      +\tchar *path;\n     -+\tstruct object_id base_oid, branch_oid;\n     ++\tstruct object_id base_oid, branch_oid, upstream_oid;\n      +\tunsigned short branch_mode;\n     ++\tenum change_state state;\n     ++};\n     ++\n     ++struct landing_search {\n     ++\tconst char *branch_name;\n     ++\tstruct commit *rev;\n     ++\tstruct commit *upstream;\n     ++\tstruct branch_change *changes;\n     ++\tsize_t changes_nr, changes_alloc;\n      +};\n      +\n     -+static void collect_branch_changes(struct commit *base, struct commit *rev,\n     -+\t\t\t\t   struct branch_change **changes,\n     -+\t\t\t\t   size_t *nr, size_t *alloc)\n     ++define_commit_slab(changed_paths, struct string_list *);\n     ++\n     ++static void collect_branch_changes(struct landing_search *search,\n     ++\t\t\t\t   struct commit *base)\n      +{\n      +\tstruct diff_options opt;\n      +\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\topt.flags.recursive = 1;\n      +\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n      +\tdiff_setup_done(&opt);\n     -+\tdiff_tree_oid(get_commit_tree_oid(base), get_commit_tree_oid(rev),\n     -+\t\t      \"\", &opt);\n     ++\tdiff_tree_oid(get_commit_tree_oid(base),\n     ++\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n      +\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n      +\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n      +\t\tstruct branch_change *change;\n      +\n     -+\t\tALLOC_GROW(*changes, *nr + 1, *alloc);\n     -+\t\tchange = &(*changes)[(*nr)++];\n     ++\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n     ++\t\t\t   search->changes_alloc);\n     ++\t\tchange = &search->changes[search->changes_nr++];\n      +\t\tchange->path = xstrdup(p->two->path);\n      +\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n      +\t\t       &p->one->oid : null_oid(the_hash_algo));\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\tdiff_flush(&opt);\n      +}\n      +\n     -+static int merge_keeps_upstream(const struct branch_change *change,\n     -+\t\t\t\tconst struct object_id *upstream_oid)\n     ++static int merge_keeps_upstream(const struct branch_change *change)\n      +{\n      +\tmmfile_t base, upstream, branch;\n      +\tmmbuffer_t result = { 0 };\n      +\tint ret;\n      +\n      +\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n     -+\tread_mmblob(&upstream, the_repository->objects, upstream_oid);\n     ++\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n      +\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n      +\tret = ll_merge(&result, change->path, &base, \"base\",\n      +\t\t       &upstream, \"upstream\", &branch, \"branch\",\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\treturn ret;\n      +}\n      +\n     -+static int change_landed(const struct branch_change *change,\n     -+\t\t\t struct commit *commit)\n     ++static enum change_state change_state_at(struct branch_change *change,\n     ++\t\t\t\t\t struct commit *commit)\n      +{\n     -+\tstruct object_id oid;\n      +\tunsigned short mode;\n      +\n      +\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n     -+\t\t\t   change->path, &oid, &mode))\n     -+\t\treturn is_null_oid(&change->branch_oid);\n     -+\tif (oideq(&oid, &change->branch_oid))\n     -+\t\treturn mode == change->branch_mode;\n     ++\t\t\t   change->path, &change->upstream_oid, &mode))\n     ++\t\treturn is_null_oid(&change->branch_oid) ?\n     ++\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n     ++\tif (oideq(&change->upstream_oid, &change->branch_oid))\n     ++\t\treturn mode == change->branch_mode ?\n     ++\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n      +\tif (is_null_oid(&change->base_oid) ||\n      +\t    is_null_oid(&change->branch_oid) ||\n     -+\t    oideq(&oid, &change->base_oid) ||\n     ++\t    oideq(&change->upstream_oid, &change->base_oid) ||\n      +\t    mode != change->branch_mode || !S_ISREG(mode))\n     -+\t\treturn 0;\n     -+\treturn merge_keeps_upstream(change, &oid);\n     ++\t\treturn CHANGE_MISSING;\n     ++\treturn CHANGE_NEEDS_MERGE;\n      +}\n      +\n     -+static struct commit *find_landed_commit(struct commit *rev,\n     -+\t\t\t\t\t struct commit *upstream)\n     ++static int changes_landed(struct landing_search *search,\n     ++\t\t\t  struct commit *commit)\n     ++{\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     ++\t\tstruct branch_change *change = &search->changes[i];\n     ++\n     ++\t\tchange->state = change_state_at(change, commit);\n     ++\t\tif (change->state == CHANGE_MISSING)\n     ++\t\t\treturn 0;\n     ++\t}\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     ++\t\tstruct branch_change *change = &search->changes[i];\n     ++\n     ++\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n     ++\t\t    !merge_keeps_upstream(change))\n     ++\t\t\treturn 0;\n     ++\t}\n     ++\treturn 1;\n     ++}\n     ++\n     ++static struct string_list *changed_paths_of(struct changed_paths *cache,\n     ++\t\t\t\t\t    struct diff_options *opt,\n     ++\t\t\t\t\t    struct commit *commit)\n     ++{\n     ++\tstruct string_list **paths = changed_paths_at(cache, commit);\n     ++\tstruct commit *parent;\n     ++\n     ++\tif (*paths)\n     ++\t\treturn *paths;\n     ++\n     ++\tCALLOC_ARRAY(*paths, 1);\n     ++\tstring_list_init_dup(*paths);\n     ++\tparent = commit->parents ? commit->parents->item : NULL;\n     ++\tif (parent && repo_parse_commit(the_repository, parent))\n     ++\t\tdie(_(\"could not parse commit %s\"),\n     ++\t\t    oid_to_hex(&parent->object.oid));\n     ++\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n     ++\t\t      get_commit_tree_oid(commit), \"\", opt);\n     ++\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n     ++\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n     ++\n     ++\t\tstring_list_append(*paths, p->two->path);\n     ++\t}\n     ++\tdiff_flush(opt);\n     ++\tstring_list_sort(*paths);\n     ++\treturn *paths;\n     ++}\n     ++\n     ++static void free_changed_paths(struct string_list **paths)\n     ++{\n     ++\tif (!*paths)\n     ++\t\treturn;\n     ++\tstring_list_clear(*paths, 0);\n     ++\tfree(*paths);\n     ++}\n     ++\n     ++static int touches_changes(struct string_list *paths,\n     ++\t\t\t   const struct landing_search *search)\n     ++{\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++)\n     ++\t\tif (string_list_has_string(paths, search->changes[i].path))\n     ++\t\t\treturn 1;\n     ++\treturn 0;\n     ++}\n     ++\n     ++static struct commit *find_landed_commit(struct landing_search *search,\n     ++\t\t\t\t\t struct changed_paths *cache,\n     ++\t\t\t\t\t struct diff_options *opt)\n      +{\n     -+\tstruct commit_list *merge_bases = NULL;\n     -+\tstruct branch_change *changes = NULL;\n     -+\tsize_t changes_nr = 0, changes_alloc = 0;\n      +\tstruct commit *commit, *landed = NULL;\n      +\tstruct strvec args = STRVEC_INIT;\n      +\tstruct rev_info revs;\n      +\n     -+\tif (repo_get_merge_bases(the_repository, upstream, rev,\n     -+\t\t\t\t &merge_bases) < 0)\n     -+\t\texit(128);\n     -+\tif (!merge_bases)\n     -+\t\treturn NULL;\n     -+\tcollect_branch_changes(merge_bases->item, rev, &changes,\n     -+\t\t\t       &changes_nr, &changes_alloc);\n     -+\tcommit_list_free(merge_bases);\n     -+\tif (!changes_nr)\n     -+\t\treturn NULL;\n     -+\n      +\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n     -+\t\t     oid_to_hex(&upstream->object.oid), NULL);\n     -+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&rev->object.oid));\n     -+\tstrvec_push(&args, \"--\");\n     -+\tfor (size_t i = 0; i < changes_nr; i++)\n     -+\t\tstrvec_pushf(&args, \":(literal)%s\", changes[i].path);\n     ++\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n     ++\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n      +\n      +\trepo_init_revisions(the_repository, &revs, NULL);\n      +\tsetup_revisions_from_strvec(&args, &revs, NULL);\n      +\tif (prepare_revision_walk(&revs))\n      +\t\tdie(_(\"revision walk setup failed\"));\n     -+\twhile (!landed && (commit = get_revision(&revs))) {\n     -+\t\tsize_t i;\n     -+\n     -+\t\tfor (i = 0; i < changes_nr; i++)\n     -+\t\t\tif (!change_landed(&changes[i], commit))\n     -+\t\t\t\tbreak;\n     -+\t\tif (i == changes_nr)\n     ++\twhile (!landed && (commit = get_revision(&revs)))\n     ++\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n     ++\t\t\t\t    search) &&\n     ++\t\t    changes_landed(search, commit))\n      +\t\t\tlanded = commit;\n     -+\t}\n      +\trelease_revisions(&revs);\n     -+\tclear_commit_marks(upstream, ALL_REV_FLAGS);\n     -+\tclear_commit_marks(rev, ALL_REV_FLAGS);\n     ++\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n     ++\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n      +\tstrvec_clear(&args);\n     -+\n     -+\tfor (size_t i = 0; i < changes_nr; i++)\n     -+\t\tfree(changes[i].path);\n     -+\tfree(changes);\n      +\treturn landed;\n      +}\n     ++\n     ++static void find_landed_commits(struct landing_search *searches,\n     ++\t\t\t\tsize_t searches_nr,\n     ++\t\t\t\tstruct strmap *landed_commits)\n     ++{\n     ++\tstruct changed_paths cache;\n     ++\tstruct diff_options opt;\n     ++\n     ++\tfor (size_t i = 0; i < searches_nr; i++) {\n     ++\t\tstruct landing_search *search = &searches[i];\n     ++\t\tstruct commit_list *merge_bases = NULL;\n     ++\n     ++\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n     ++\t\t\t\t\t search->rev, &merge_bases) < 0)\n     ++\t\t\texit(128);\n     ++\t\tif (merge_bases)\n     ++\t\t\tcollect_branch_changes(search, merge_bases->item);\n     ++\t\tcommit_list_free(merge_bases);\n     ++\t}\n     ++\n     ++\trepo_diff_setup(the_repository, &opt);\n     ++\topt.flags.recursive = 1;\n     ++\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n     ++\topt.no_free = 1;\n     ++\tdiff_setup_done(&opt);\n     ++\tinit_changed_paths(&cache);\n     ++\n     ++\tfor (size_t i = 0; i < searches_nr; i++) {\n     ++\t\tstruct commit *landed;\n     ++\n     ++\t\tif (!searches[i].changes_nr)\n     ++\t\t\tcontinue;\n     ++\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n     ++\t\tif (landed)\n     ++\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n     ++\t\t\t\t   landed);\n     ++\t}\n     ++\n     ++\tdeep_clear_changed_paths(&cache, free_changed_paths);\n     ++\topt.no_free = 0;\n     ++\tdiff_free(&opt);\n     ++}\n     ++\n     ++static int branch_opted_out(struct strbuf *key, const char *branch_name,\n     ++\t\t\t    unsigned int flags)\n     ++{\n     ++\tint opt_out;\n     ++\n     ++\tstrbuf_reset(key);\n     ++\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n     ++\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n     ++\t    opt_out)\n     ++\t\treturn 0;\n     ++\tif (!(flags & DELETE_BRANCH_QUIET))\n     ++\t\tfprintf(stderr,\n     ++\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n     ++\t\t\tbranch_name, branch_name);\n     ++\treturn 1;\n     ++}\n      +\n       static int delete_merged_branches(const struct strvec *upstreams,\n       \t\t\t\t const char **argv, unsigned int flags)\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \tstruct strset deletable_branch_names = STRSET_INIT;\n       \tstruct strset protected_branch_names = STRSET_INIT;\n      +\tstruct strmap landed_commits = STRMAP_INIT;\n     ++\tstruct landing_search *searches = NULL;\n     ++\tsize_t searches_nr = 0, searches_alloc = 0;\n       \tstruct strvec branches_to_delete = STRVEC_INIT;\n       \tstruct strbuf key = STRBUF_INIT;\n       \tstruct hashmap_iter iter;\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \t\tconst char *branch_name;\n       \t\tstruct branch *branch;\n       \t\tconst char *upstream_refname;\n     -+\t\tstruct commit *landed = NULL;\n     - \t\tint opt_out;\n     +-\t\tint opt_out;\n       \n       \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n     + \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n      @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n       \t\t\tcontinue;\n       \t\tif (check_branch_commit(branch_name, branch_name,\n       \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n      -\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n      -\t\t\tcontinue;\n     +-\n     +-\t\tstrbuf_reset(&key);\n     +-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n     +-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n     +-\t\t    !opt_out) {\n     +-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n     +-\t\t\t\tfprintf(stderr,\n     +-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n     +-\t\t\t\t\tbranch_name, branch_name);\n      +\t\t\t\t\tFILTER_REFS_BRANCHES,\n      +\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n      +\t\t\tstruct commit *rev = lookup_commit_reference(\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n      +\t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n      +\t\t\t\tupstream_refname);\n      +\n     -+\t\t\tif (!rev || !upstream ||\n     -+\t\t\t    !(landed = find_landed_commit(rev, upstream)))\n     ++\t\t\tif (!rev || !upstream)\n      +\t\t\t\tcontinue;\n     -+\t\t}\n     - \n     - \t\tstrbuf_reset(&key);\n     - \t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n     -@@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n     ++\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n     ++\t\t\tsearches[searches_nr++] = (struct landing_search) {\n     ++\t\t\t\t.branch_name = branch_name,\n     ++\t\t\t\t.rev = rev,\n     ++\t\t\t\t.upstream = upstream,\n     ++\t\t\t};\n     + \t\t\tcontinue;\n       \t\t}\n       \n     - \t\tstrset_add(&deletable_branch_names, branch_name);\n     -+\t\tif (landed)\n     -+\t\t\tstrmap_put(&landed_commits, branch_name, landed);\n     +-\t\tstrset_add(&deletable_branch_names, branch_name);\n     ++\t\tif (!branch_opted_out(&key, branch_name, flags))\n     ++\t\t\tstrset_add(&deletable_branch_names, branch_name);\n     ++\t}\n     ++\n     ++\tfind_landed_commits(searches, searches_nr, &landed_commits);\n     ++\tfor (size_t i = 0; i < searches_nr; i++) {\n     ++\t\tconst char *branch_name = searches[i].branch_name;\n     ++\n     ++\t\tif (strmap_contains(&landed_commits, branch_name) &&\n     ++\t\t    !branch_opted_out(&key, branch_name, flags))\n     ++\t\t\tstrset_add(&deletable_branch_names, branch_name);\n       \t}\n       \n       \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \n       \tstrbuf_release(&key);\n       \tstrvec_clear(&branches_to_delete);\n     ++\tfor (size_t i = 0; i < searches_nr; i++) {\n     ++\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n     ++\t\t\tfree(searches[i].changes[j].path);\n     ++\t\tfree(searches[i].changes);\n     ++\t}\n     ++\tfree(searches);\n      +\tstrmap_clear(&landed_commits, 0);\n       \tstrset_clear(&protected_branch_names);\n       \tstrset_clear(&deletable_branch_names);\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +test_expect_success '--delete-merged deletes a squash merged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic squashed squashed-one squashed-two &&\n     -+\tpush_topic partial partial-landed partial-pending &&\n     ++\tpush_topic partial lands-upstream stays-local &&\n      +\tsquash_merge_upstream partial~1 &&\n      +\tsquash_merge_upstream squashed &&\n      +\tsquash=$(git -C upstream rev-parse --short next) &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t\tEOF\n      +\t)\n      +'\n     ++\n     ++test_expect_success '--delete-merged deletes a squash merged branch after upstream changed the same file' '\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n     ++\t\tgit add shared &&\n     ++\t\tgit commit -m \"add shared\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\tsetup_repo_for_delete_merged &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit checkout -b edits-shared --track origin/next &&\n     ++\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n     ++\t\tgit commit -a -m \"edit line 2\" &&\n     ++\t\tgit push origin edits-shared &&\n     ++\t\tgit checkout --detach\n     ++\t) &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n     ++\t\tgit commit -a -m \"edit line 5\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\tsquash_merge_upstream edits-shared &&\n     ++\tsquash=$(git -C upstream rev-parse --short next) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\t\tsha=$(git rev-parse --short edits-shared) &&\n     ++\n     ++\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n     ++\t\ttest_cmp expect actual &&\n     ++\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\tmain\n     ++\t\tEOF\n     ++\t)\n     ++'\n     ++\n     ++test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n     ++\tsetup_repo_for_delete_merged &&\n     ++\tpush_topic a-unmerged unmerged-work &&\n     ++\tpush_topic b-merged merged-work &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_commit upstream-work &&\n     ++\t\tgit merge --no-ff b-merged &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\t\tsha=$(git rev-parse --short b-merged) &&\n     ++\n     ++\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n     ++\t\ttest_cmp expect actual &&\n     ++\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\ta-unmerged\n     ++\t\tmain\n     ++\t\tEOF\n     ++\t)\n     ++'\n      +\n       test_expect_success '--delete-merged keeps main despite a different default push remote' '\n       \tsetup_repo_for_delete_merged &&\n -:  ---------- > 2:  ec88bde252 branch: add --no-squashed to skip the squash search\n\n-- \ngitgitgadget\n\n"},{"id":"554711","messageId":"94a6d23f8b96d4a07fb58c71f4a2f7dc4d3055bc.1791711665.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v2.git.git.1791711665.gitgitgadget@gmail.com","subject":"[PATCH v2 1/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-11T09:41:04Z","receivedAt":"2026-10-11T09:41:04Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". The same goes for a\nbranch that was rebased or cherry-picked onto upstream. The upstream\nholds a rewritten copy of its work, so its tip is not reachable from\nit and it looks unmerged forever.\n\nThis happens all the time on hosting services. The \"Squash and merge\"\nand \"Rebase and merge\" buttons on GitHub land pull requests this way,\nso contributors to such projects collect local branches that\n\"--delete-merged\" never cleans up.\n\nTreat such a branch as merged when some upstream commit since the fork\npoint contains all of its changes, so that merging the branch into\nthat commit would change nothing. Name that commit in the output so\nthe user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first upstream commit that contains the changes is used, so the\nbranch is deleted even if upstream later reverted or reworked them.\nNothing is lost, since that commit keeps them in the upstream history.\nA branch whose changes only partly landed is kept.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc |  15 +-\n builtin/branch.c              | 332 ++++++++++++++++++++++++++++++++--\n t/t3200-branch.sh             | 143 +++++++++++++++\n 3 files changed, 466 insertions(+), 24 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..0427324de1 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -204,12 +204,15 @@ This option is only applicable in non-verbose mode.\n \n `--delete-merged <pattern>`::\n \tDelete local branches whose configured upstream matches\n-\t_<pattern>_, but only when their tip is reachable from that\n-\tupstream. In other words, the work on the branch has already\n-\tlanded on the upstream it tracks, so the local copy is no longer\n-\tneeded. _<pattern>_ may name a ref, a remote (using the branch its\n-\t`HEAD` points at), or a shell-style glob. The option can be\n-\trepeated to widen the upstream match.\n+\t_<pattern>_, but only when their work has already landed on that\n+\tupstream, so the local copy is no longer needed. This is the case\n+\twhen their tip is reachable from the upstream, or when some\n+\tupstream commit contains all of their changes, as happens after\n+\ta squash or rebase merge, even if those changes were later\n+\treverted. The message for such a branch names the first upstream\n+\tcommit that contains its changes. _<pattern>_ may name a ref, a\n+\tremote (using the branch its `HEAD` points at), or a shell-style\n+\tglob. The option can be repeated to widen the upstream match.\n \tOptional _<branch-pattern>_ arguments limit which local branches\n \tare considered, e.g. `git branch --delete-merged 'origin/*'\n \t'topic-*'`.\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..513c39763b 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,14 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"merge-ll.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -236,7 +244,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +342,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +367,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -824,6 +852,251 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n+\tunsigned short branch_mode;\n+\tenum change_state state;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options opt;\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&opt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&opt);\n+}\n+\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmbuffer_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n+{\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n+\t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *opt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(opt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *opt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options opt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\topt.no_free = 1;\n+\tdiff_setup_done(&opt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\topt.no_free = 0;\n+\tdiff_free(&opt);\n+}\n+\n+static int branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t    unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn 0;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn 1;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -832,6 +1105,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -852,7 +1128,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -867,21 +1142,35 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n-\t\t\tcontinue;\n-\n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n+\t\t\t\tupstream_refname);\n+\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n \t\t}\n \n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n@@ -895,7 +1184,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -903,6 +1192,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1135,7 +1431,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..adf1bc2233 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1979,6 +1979,149 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-merged deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n-- \ngitgitgadget\n\n\n"},{"id":"554712","messageId":"ec88bde25273700c70a547593ac8f68046218408.1791711665.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v2.git.git.1791711665.gitgitgadget@gmail.com","subject":"[PATCH v2 2/2] branch: add --no-squashed to skip the squash search","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-11T09:41:05Z","receivedAt":"2026-10-11T09:41:05Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nFinding branches that were squash or rebase merged makes \"git branch\n--delete-merged\" slower, and in a partial clone it may have to\ndownload blobs. Users who do not need it have no way to skip that\nsearch.\n\nAdd --no-squashed, which only deletes branches whose tip is reachable\nfrom their upstream.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc |  8 +++++++-\n builtin/branch.c              | 12 ++++++++++--\n t/t3200-branch.sh             | 27 +++++++++++++++++++++++++++\n 3 files changed, 44 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 0427324de1..90960384c4 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -25,7 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch>\n git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n-git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] [--no-squashed] (--delete-merged <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -244,6 +244,12 @@ remove it anyway.\n \tsanity-checking a wide pattern like `'origin/*'` before\n \tcommitting to the deletion.\n \n+`--no-squashed`::\n+\tWith `--delete-merged`, only delete branches whose tip is\n+\treachable from their upstream. This skips the search for\n+\tbranches that were squash or rebase merged, which takes longer\n+\tand may have to download blobs in a partial clone.\n+\n `-v`::\n `-vv`::\n `--verbose`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 513c39763b..96f10a7546 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -209,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_NO_SQUASHED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -1149,7 +1150,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n \t\t\t\tupstream_refname);\n \n-\t\t\tif (!rev || !upstream)\n+\t\t\tif ((flags & DELETE_BRANCH_NO_SQUASHED) ||\n+\t\t\t    !rev || !upstream)\n \t\t\t\tcontinue;\n \t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n \t\t\tsearches[searches_nr++] = (struct landing_search) {\n@@ -1272,6 +1274,7 @@ int cmd_branch(int argc,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n \tint dry_run = 0;\n+\tint no_squashed = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n \t/* possible options */\n@@ -1330,6 +1333,8 @@ int cmd_branch(int argc,\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n \t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\tOPT_BOOL(0, \"no-squashed\", &no_squashed,\n+\t\t\tN_(\"with --delete-merged, skip the search for squash merged branches\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1394,6 +1399,8 @@ int cmd_branch(int argc,\n \n \tif (dry_run && !delete_merged.nr)\n \t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (no_squashed && !delete_merged.nr)\n+\t\tdie(_(\"--no-squashed requires --delete-merged\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1436,7 +1443,8 @@ int cmd_branch(int argc,\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n-\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     (no_squashed ? DELETE_BRANCH_NO_SQUASHED : 0));\n \t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex adf1bc2233..70a38663f1 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2095,6 +2095,28 @@ test_expect_success '--delete-merged deletes a squash merged branch after upstre\n \t)\n '\n \n+test_expect_success '--delete-merged --no-squashed deletes only reachable branches' '\n+\tsetup_repo_for_delete_merged &&\n+\tcreate_merged_branch merged &&\n+\tpush_topic squash-merged squash-merged-work &&\n+\tsquash_merge_upstream squash-merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short merged) &&\n+\n+\t\tgit branch --delete-merged origin/next --no-squashed \\\n+\t\t\t>actual 2>&1 &&\n+\t\techo \"Deleted branch merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tsquash-merged\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n@@ -2316,6 +2338,11 @@ test_expect_success \"branch -d still deletes a deleteMerged=false branch\" '\n \t)\n '\n \n+test_expect_success '--no-squashed without --delete-merged is rejected' '\n+\ttest_must_fail git -C forked branch --no-squashed 2>err &&\n+\ttest_grep \"requires --delete-merged\" err\n+'\n+\n test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_must_fail git -C forked branch --dry-run 2>err &&\n \ttest_grep \"requires --delete-merged\" err\n-- \ngitgitgadget\n\n"},{"id":"554731","messageId":"xmqq1p9vdalz.fsf@gitster.g","threadId":"66417","inReplyTo":"pull.2425.v2.git.git.1791711665.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-12T00:30:00Z","receivedAt":"2026-10-12T00:30:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\n> never deleted by git branch --delete-merged, because their tips are not\n> reachable from the upstream. This treats such a branch as merged when some\n> upstream commit contains all of its changes, and names that commit in the\n> output:\n>\n> Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nInteresting.\n\nAfter rebasing, you probably can (re)use the same algorithm as used\nin range-diff to match the commits and their rewritten counterparts,\nbut I wonder how \"squash\" would go.  Presumably you can locally make\na trial squash merge and probe for a similar commit on the upstream\ncommit.\n\nOn the other hand, unlike deliberate rebasing done during a patch\niteration, rebase done on the upstream to integrate your changes\nwould not rewrite the log messages, so matching the commits based on\nthe log message may work better than a range-diff between iteration\nvN and v(N+1) in the submitted patches.\n\n>  * Diff each upstream commit once and share the result between all branches,\n>    faster than v1.\n>  * Fix a branch merged with a merge commit being reported as \"landed as\" the\n>    merge when another branch was searched first, and add a test for it. Show\n>    the topic commit itself in \"landed as\" for work that arrived through a\n>    merge.\n>  * Use git merge --squash as the example in the commit message and mention\n>    GitHub's squash and rebase buttons.\n>  * Add --no-squashed to skip the squash search.\n>\n> Harald Nordgren (2):\n>   branch: let --delete-merged find squash merged branches\n>   branch: add --no-squashed to skip the squash search\n\nThis a bit unexpected two-patch organization; I would have expected\nthat the cases where our side and upstream side should correspond\none-to-one (i.e., what you call rebase-and-merge) and other cases\n(i.e., squash merge) to require very different matching algorithm,\nwhich would lead to two-patch organization where one adds support\nfor rebase and the other adds support for squash.\n\n\n"},{"id":"554749","messageId":"03b187ae-bef2-426f-b6a4-def9f70c4a3f@gmail.com","threadId":"66417","inReplyTo":"xmqq1p9vdalz.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-12T09:50:43Z","receivedAt":"2026-10-12T09:50:43Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 12/10/2026 01:30, Junio C Hamano wrote:\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> >> Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\n>> never deleted by git branch --delete-merged, because their tips are not\n>> reachable from the upstream. This treats such a branch as merged when some\n>> upstream commit contains all of its changes, and names that commit in the\n>> output:\n>>\n>> Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n> > Interesting.\n> > After rebasing, you probably can (re)use the same algorithm as used\n> in range-diff to match the commits and their rewritten counterparts,\n> but I wonder how \"squash\" would go.  Presumably you can locally make\n> a trial squash merge and probe for a similar commit on the upstream\n> commit.\n> > On the other hand, unlike deliberate rebasing done during a patch\n> iteration, rebase done on the upstream to integrate your changes\n> would not rewrite the log messages, so matching the commits based on\n> the log message may work better than a range-diff between iteration\n> vN and v(N+1) in the submitted patches.\n\nI think it would be helpful if the cover letter explained in more detail what this series actually does to detect squashed merges and compared that to other approaches such as using patch ids or test merges. As far as I can see the approach here is, to a first approximation, checking\n\n\t$(git merge-tree B C) == C^{tree}\n\nto see if C is a squashed merge of B, only without using merge-ort but by calling merge_ll() on each path instead. That means we're not checking if C is the same as merging B into C^, C may contain additional changes as well. I think it also means we're missing out on rename detection. It would be good to get a clear idea of what the tradeoffs of the various approaches are before any more code is written.\n\nThanks\n\nPhillip\n\n>>   * Diff each upstream commit once and share the result between all branches,\n>>     faster than v1.\n>>   * Fix a branch merged with a merge commit being reported as \"landed as\" the\n>>     merge when another branch was searched first, and add a test for it. Show\n>>     the topic commit itself in \"landed as\" for work that arrived through a\n>>     merge.\n>>   * Use git merge --squash as the example in the commit message and mention\n>>     GitHub's squash and rebase buttons.\n>>   * Add --no-squashed to skip the squash search.\n>>\n>> Harald Nordgren (2):\n>>    branch: let --delete-merged find squash merged branches\n>>    branch: add --no-squashed to skip the squash search\n> > This a bit unexpected two-patch organization; I would have expected\n> that the cases where our side and upstream side should correspond\n> one-to-one (i.e., what you call rebase-and-merge) and other cases\n> (i.e., squash merge) to require very different matching algorithm,\n> which would lead to two-patch organization where one adds support\n> for rebase and the other adds support for squash.\n\n\n"},{"id":"554755","messageId":"CAHwyqnV=SBMwTzC74YN7585u_pd525E=tRArL_5z_uLb-FGnhg@mail.gmail.com","threadId":"66417","inReplyTo":"xmqq1p9vdalz.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-12T12:41:40Z","receivedAt":"2026-10-12T12:41:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> After rebasing, you probably can (re)use the same algorithm as used\n> in range-diff to match the commits and their rewritten counterparts,\n> but I wonder how \"squash\" would go.  Presumably you can locally make\n> a trial squash merge and probe for a similar commit on the upstream\n> commit.\n>\n> On the other hand, unlike deliberate rebasing done during a patch\n> iteration, rebase done on the upstream to integrate your changes\n> would not rewrite the log messages, so matching the commits based on\n> the log message may work better than a range-diff between iteration\n> vN and v(N+1) in the submitted patches.\n\nMatching on commit messages sounds very brittle to me (GitHub mangles\nthem on squash), and also unnecessary when we can compare the files\nthemselves.\n\n> > Harald Nordgren (2):\n> >   branch: let --delete-merged find squash merged branches\n> >   branch: add --no-squashed to skip the squash search\n>\n> This a bit unexpected two-patch organization; I would have expected\n> that the cases where our side and upstream side should correspond\n> one-to-one (i.e., what you call rebase-and-merge) and other cases\n> (i.e., squash merge) to require very different matching algorithm,\n> which would lead to two-patch organization where one adds support\n> for rebase and the other adds support for squash.\n\nFinding squashes is a superset of finding rebase merges. Although\nslower, which is why we need to be able to turn it off. So yes, then\nwe need separate the code paths for squash and merge finding.\n\nThen I'm thinking 3 commits:\n\n    branch: let --delete-merged find rebase merged branches\n    branch: let --delete-merged find squash merged branches\n    branch: add --no-squashed to skip the squash merge check\n\n\nHarald\n\n"},{"id":"554758","messageId":"CAHwyqnUP8Bgd4S=oE9g0TonqL-ohy4LXXG66AY8SeVk=6HCNUg@mail.gmail.com","threadId":"66417","inReplyTo":"03b187ae-bef2-426f-b6a4-def9f70c4a3f@gmail.com","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-12T14:31:22Z","receivedAt":"2026-10-12T14:31:22Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> I think it would be helpful if the cover letter explained in more detail\n> what this series actually does to detect squashed merges and compared\n> that to other approaches such as using patch ids or test merges.\n\nOk.\n\n> As far\n> as I can see the approach here is, to a first approximation, checking\n>\n>         $(git merge-tree B C) == C^{tree}\n>\n> to see if C is a squashed merge of B, only without using merge-ort but\n> by calling merge_ll() on each path instead. That means we're not\n> checking if C is the same as merging B into C^, C may contain additional\n> changes as well. I think it also means we're missing out on rename\n> detection.\n\nThe check is \"does C already contain everything B changed\", not \"is C\nexactly the squash of B\". For deleting the branch it could be enough\nsince nothing is lost.\n\nBut it means \"landed as\" can point to a commit with other changes in\nit as well, maybe that's not good.\n\nI think following renames is a bad basis for assuming merged.\nSometimes the goal of a topic is a rename, and then it should not be\ndropped.\n\n\nHarald\n\n"},{"id":"554774","messageId":"CALnO6CDTaXcfDArqT3tj3fY5hReEcAaMmCbTD9fpMJdbCMc-bA@mail.gmail.com","threadId":"66417","inReplyTo":"CAHwyqnV=SBMwTzC74YN7585u_pd525E=tRArL_5z_uLb-FGnhg@mail.gmail.com","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-12T15:49:00Z","receivedAt":"2026-10-12T15:49:00Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Mon, Oct 12, 2026 at 8:42 AM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> > After rebasing, you probably can (re)use the same algorithm as used\n> > in range-diff to match the commits and their rewritten counterparts,\n> > but I wonder how \"squash\" would go.  Presumably you can locally make\n> > a trial squash merge and probe for a similar commit on the upstream\n> > commit.\n> >\n> > On the other hand, unlike deliberate rebasing done during a patch\n> > iteration, rebase done on the upstream to integrate your changes\n> > would not rewrite the log messages, so matching the commits based on\n> > the log message may work better than a range-diff between iteration\n> > vN and v(N+1) in the submitted patches.\n>\n> Matching on commit messages sounds very brittle to me (GitHub mangles\n> them on squash), and also unnecessary when we can compare the files\n> themselves.\n\nWell, I think Junio was talking about the rebase cases for log\nmessages, but I have been known to cherry-pick and edit the log\nmessage of small contributions that aren't up to my personal standards\nto help contributors land their changes, in which cases you *would*\ncare more about patch ID than log message.\n\n-- \nD. Ben Knoble\n\n"},{"id":"554777","messageId":"xmqq4ieqamn0.fsf@gitster.g","threadId":"66417","inReplyTo":"03b187ae-bef2-426f-b6a4-def9f70c4a3f@gmail.com","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-12T16:50:43Z","receivedAt":"2026-10-12T16:50:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> detection. It would be good to get a clear idea of what the tradeoffs of \n> the various approaches are before any more code is written.\n\nIt sounds like a very sensible approach.\n\nAt least the one-to-one part of the operation should learn from what\nDscho did in range-diff, especially the part that does take\nsimilarity of the log messages and authorship information, instead\nof paying attention only to patch IDs.  I am not convinced that\none-to-one rebase implemented as a subset of squash merge would\nperform sufficiently well, either.  From the user interface angle,\nit also is not clear to me if a single --delete-merged that enables\nall variants by default with individual knob to optionally disable\none variant (squash) would be a good extensible one, when we may\nneed to deal with the third (and other) kinds of ways to integrate\nyour work without preserving the commit identity, like selective\ncherry-picks.\n\n\n\n"},{"id":"554779","messageId":"CAHwyqnUP72njKKap16u+kShHmhssiUXFUVRhURPP_wzwZsvtdg@mail.gmail.com","threadId":"66417","inReplyTo":"xmqq4ieqamn0.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-12T17:44:22Z","receivedAt":"2026-10-12T17:44:22Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> At least the one-to-one part of the operation should learn from what\n> Dscho did in range-diff\n\nOh, that's great! I'll take a look.\n\n> especially the part that does take\n> similarity of the log messages and authorship information, instead\n> of paying attention only to patch IDs.\n\nRight, authorship would at least be a good filter to speed things up,\ncan't be the same commit if it has two different authors!\n\n\nHarald\n\n"},{"id":"554824","messageId":"xmqqy0c21gq1.fsf@gitster.g","threadId":"66417","inReplyTo":"CAHwyqnUP72njKKap16u+kShHmhssiUXFUVRhURPP_wzwZsvtdg@mail.gmail.com","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-13T02:23:18Z","receivedAt":"2026-10-13T02:23:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n>> At least the one-to-one part of the operation should learn from what\n>> Dscho did in range-diff\n>\n> Oh, that's great! I'll take a look.\n>\n>> especially the part that does take\n>> similarity of the log messages and authorship information, instead\n>> of paying attention only to patch IDs.\n>\n> Right, authorship would at least be a good filter to speed things up,\n> can't be the same commit if it has two different authors!\n\nAfter thinking about it a bit more, I no longer am sure.\n\nIt eventually boils down to what we are trying to achieve with this\noption, but \"I have these pending works I asked upstream to take a\nlook, I want to see which ones landed and remove them from my local\ncopy of the project\" and \"I have these pending works, but upstream\nhas been collecting improvements from many other people in addition\nto me.  I want to see which ones are still relevant\" are overlapping\nbut different.  And I think when you start talking about rebasing\nand squash merging, you are asking \"is my copy still relevant?\"\n\nAnd the latter is strict superset of the former.\n\nYou might have instructed your agent to write a two patch series and\nasked upstream to pull.  Somebody else used the same AI-gen and\ncreated one patch that solves the same problem, which happened to\nhave landed first.  Or that somebody may have sent their solution in\ntwo patches similar to yours.  In either case, your two patch series\nis now irrelevant.  You would want to drop your branch that is no\nlonger relevant, regardless of whose work made it irrelevant,\n\nThey may be textually similar enough that we can tell mechanically\nthat there are already the same or similar code in the place you\nwere trying to touch.  \"--delete-irrelevant\" would remove your local\ncopy.  There may be salvageable bits (e.g., yours may have updates\nto documentation and tests that theirs lacks), so if we can detect a\ncase where only some parts of your work has become irrelevant, that\nwould be very useful, too.\n\nStepping back a bit, perhaps \"--delete-merged\" should not be\nmodified to touch anything that was rebased, cherry-picked, or\nsquash merged.  It is good to have one stable and strict \"if my\ncommits were merged and safely saved there, delete them from here\",\nregardless of how much looser definition of \"the same patch\" is used\nfor \"--delete-irrelevant\".\n\n\n"},{"id":"555337","messageId":"CAHwyqnUv_HDHobwSMuK5vxFBZT9zstyyQJpqTv_=o_XhA7TJZw@mail.gmail.com","threadId":"66417","inReplyTo":"xmqqy0c21gq1.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-17T22:23:11Z","receivedAt":"2026-10-17T22:23:11Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> You might have instructed your agent to write a two patch series and\n> asked upstream to pull.  Somebody else used the same AI-gen and\n> created one patch that solves the same problem, which happened to\n> have landed first.  Or that somebody may have sent their solution in\n> two patches similar to yours.  In either case, your two patch series\n> is now irrelevant.  You would want to drop your branch that is no\n> longer relevant, regardless of whose work made it irrelevant,\n>\n> They may be textually similar enough that we can tell mechanically\n> that there are already the same or similar code in the place you\n> were trying to touch.  \"--delete-irrelevant\" would remove your local\n> copy.  There may be salvageable bits (e.g., yours may have updates\n> to documentation and tests that theirs lacks), so if we can detect a\n> case where only some parts of your work has become irrelevant, that\n> would be very useful, too.\n\nI would be very surprised if my branch gets deleted because someone\nelse wrote *almost* the same code in parallel.\n\nI think we can achieve a lot here just by focusing on the use case of\nmy own work being slightly mangled (squashed or merged).\n\n> Stepping back a bit, perhaps \"--delete-merged\" should not be\n> modified to touch anything that was rebased, cherry-picked, or\n> squash merged.  It is good to have one stable and strict \"if my\n> commits were merged and safely saved there, delete them from here\",\n> regardless of how much looser definition of \"the same patch\" is used\n> for \"--delete-irrelevant\".\n\nIf my branch gets picked up by the mainline, my work there is done. I\nsee no reason to keep the branch around anymore. How it gets picked up\n(squash, merged, or preserved commit hash) is not relevant then\nanymore.\n\nBesides, I can easily find the work again if I want, since by\ndefinition it's now in the mainline :)\n\n\nHarald\n\n"},{"id":"555351","messageId":"CAHwyqnXNyM+qaxggQOiW+jE5U6B=jOOcv=KGz-2XS7jnB2j-uw@mail.gmail.com","threadId":"66417","inReplyTo":"xmqq4ieqamn0.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-18T15:09:51Z","receivedAt":"2026-10-18T15:09:51Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> At least the one-to-one part of the operation should learn from what\n> Dscho did in range-diff, especially the part that does take\n> similarity of the log messages and authorship information, instead\n> of paying attention only to patch IDs.  I am not convinced that\n> one-to-one rebase implemented as a subset of squash merge would\n> perform sufficiently well, either.  From the user interface angle,\n> it also is not clear to me if a single --delete-merged that enables\n> all variants by default with individual knob to optionally disable\n> one variant (squash) would be a good extensible one, when we may\n> need to deal with the third (and other) kinds of ways to integrate\n> your work without preserving the commit identity, like selective\n> cherry-picks.\n\nI tried a range-diff solution locally, pairing commits with range-diff\nfor rebases (and moving the squash search behind an opt-in flag). As\nfar as I can tell range-diff pairs commits on the diff alone, and the\nlog message and author only show up in its output, so it didn't help\nwith matching here.\n\nOn speed it went the other way. On my clone with 65 branches, v2 takes\nabout 0.5s and finds 14 branches to delete. The range-diff version\ntakes about 10s, finds the same.\n\n\nHarald\n\n"},{"id":"555373","messageId":"20261019044005.99247-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"xmqqy0c21gq1.fsf@gitster.g","subject":"Re: [PATCH v2 0/2] branch: let --delete-merged find squash merged branches","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-10-19T04:40:05Z","receivedAt":"2026-10-19T04:40:05Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Stepping back a bit, perhaps \"--delete-merged\" should not be\n> modified to touch anything that was rebased, cherry-picked, or\n> squash merged.  It is good to have one stable and strict \"if my\n> commits were merged and safely saved there, delete them from here\",\n> regardless of how much looser definition of \"the same patch\" is used\n> for \"--delete-irrelevant\".\n\nI am in the group of users this series is for. Most repositories I\nwork in land pull requests with GitHub's \"Squash and merge\". I built\nv2 (ec88bde252) and tested it on generated repositories. The results\nsupport keeping the strict meaning as the default.\n\nv2 did what it says for squash merges, GitHub-style squashes with a\nnew message and committer, rebase merges, renames, and squashes that\nwere later reverted. \"landed as\" named the correct commit every\ntime. It kept a branch whose work only partly landed.\n\nBut it also deleted a branch that was never merged. Another author\nhad made the same change upstream in an unrelated commit, and v2\nreported the branch as \"landed as\" that commit. That is the\n\"irrelevant\" case from your message, and today v2 handles it the same\nway as a squash merge of my own work.\n\nSo I agree that the strict meaning should stay the default. I also\nwant to turn the looser check on once per repository and then forget\nabout it. Something close to what Ben suggested earlier would do\nthat:\n\n    git branch --delete-merged origin/main --landed=squash\n\nplus a config variable for the default. A third kind of integration\nthen becomes one more value, not one more --no-<kind> option.\n\nOn speed: with 3056 commits and 80 branches, v2 took 0.078s against\n0.030s for 2.56.0, and --no-squashed brought it back to 0.030s. In a\nblobless clone the first run took 0.42s and fetched 13 blobs.\n--no-squashed fetched none.\n\nOne more point. A branch checked out in a worktree is skipped with no\nmessage, but a branch with deleteMerged=false prints \"Skipping\". I\nkeep one worktree per pull request, so most of my merged branches\nare skipped silently. A \"Skipping 'topic' (checked out in <path>)\"\nline would help. I plan to send a separate RFC for\n\"git worktree list --merged\", so it would also help if the \"has this\nbranch landed?\" check were a helper that worktree code can call.\n\nTested-by: Jasper Mayone <me@jaspermayone.com>\n\nThanks,\nJasper\n\n"},{"id":"555729","messageId":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T12:28:34Z","receivedAt":"2026-10-21T12:28:34Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nThis series finds the first upstream commit since the fork point that has\nthe branch's version of every file the branch changed, comparing only object\nids. With --squashed, a file that upstream also changed counts when merging\nthe branch's version into upstream's leaves it unchanged. Timed on my clone\nwith 65 branches, this takes 0.5s.\n\nOther approaches:\n\n * Full merge-ort test merge per upstream commit: about 9s, because it\n   builds a whole tree for every commit.\n * range-diff pairing for rebases: about 10s. It pairs on diffs alone,\n   ignoring log messages, so it found nothing extra.\n * Patch ids: not tried. They tell whether a commit is the same patch, but\n   deleting a branch only needs a commit that contains its changes. They\n   miss a squash that was touched up and changes next to upstream edits, and\n   they read file contents for every upstream commit touching the branch's\n   files.\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (3):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let --delete-merged find rebase merged branches\n  branch: add --squashed to find squash merged branches\n\n Documentation/git-branch.adoc |  26 ++-\n builtin/branch.c              | 345 ++++++++++++++++++++++++++++++++--\n t/t3200-branch.sh             | 192 +++++++++++++++++++\n 3 files changed, 537 insertions(+), 26 deletions(-)\n\n\nbase-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v3\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v2:\n\n -:  ---------- > 1:  82413e5cd7 branch: move the deleteMerged opt-out check into a helper\n 1:  94a6d23f8b ! 2:  9a7e88ced6 branch: let --delete-merged find squash merged branches\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    branch: let --delete-merged find squash merged branches\n     +    branch: let --delete-merged find rebase merged branches\n      \n          A branch that upstream integrated with\n      \n     -        git merge --squash topic\n     -        git commit\n     +        git rebase main topic\n     +        git checkout main\n     +        git merge --ff-only topic\n      \n     -    is never deleted by \"git branch --delete-merged\". The same goes for a\n     -    branch that was rebased or cherry-picked onto upstream. The upstream\n     -    holds a rewritten copy of its work, so its tip is not reachable from\n     -    it and it looks unmerged forever.\n     -\n     -    This happens all the time on hosting services. The \"Squash and merge\"\n     -    and \"Rebase and merge\" buttons on GitHub land pull requests this way,\n     -    so contributors to such projects collect local branches that\n     -    \"--delete-merged\" never cleans up.\n     +    is never deleted by \"git branch --delete-merged\". This is what the\n     +    \"Rebase and merge\" button on GitHub does. The upstream holds rewritten\n     +    copies of the branch's commits, so its tip is not reachable from it and\n     +    it looks unmerged forever.\n      \n          Treat such a branch as merged when some upstream commit since the fork\n     -    point contains all of its changes, so that merging the branch into\n     -    that commit would change nothing. Name that commit in the output so\n     -    the user can see where the work went:\n     +    point has the branch's version of every file the branch changed. Name\n     +    that commit in the output so the user can see where the work went:\n      \n              Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n      \n     -    The first upstream commit that contains the changes is used, so the\n     -    branch is deleted even if upstream later reverted or reworked them.\n     -    Nothing is lost, since that commit keeps them in the upstream history.\n     -    A branch whose changes only partly landed is kept.\n     +    The first upstream commit that has them is used, so the branch is\n     +    deleted even if upstream later reverted or reworked them. Nothing is\n     +    lost, since that commit keeps them in the upstream history. A branch\n     +    whose changes only partly landed is kept.\n     +\n     +    The same check finds a squash merge when upstream did not change the\n     +    same files between the fork point and the squash. It never reads file\n     +    contents, so it stays cheap in a partial clone.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/git-branch.adoc: This option is only applicable in non-verbose mod\n      +\t_<pattern>_, but only when their work has already landed on that\n      +\tupstream, so the local copy is no longer needed. This is the case\n      +\twhen their tip is reachable from the upstream, or when some\n     -+\tupstream commit contains all of their changes, as happens after\n     -+\ta squash or rebase merge, even if those changes were later\n     ++\tupstream commit has their version of every file they changed, as\n     ++\thappens after a rebase merge, even if those changes were later\n      +\treverted. The message for such a branch names the first upstream\n     -+\tcommit that contains its changes. _<pattern>_ may name a ref, a\n     -+\tremote (using the branch its `HEAD` points at), or a shell-style\n     -+\tglob. The option can be repeated to widen the upstream match.\n     ++\tcommit that has them. _<pattern>_ may name a ref, a remote (using\n     ++\tthe branch its `HEAD` points at), or a shell-style glob. The\n     ++\toption can be repeated to widen the upstream match.\n       \tOptional _<branch-pattern>_ arguments limit which local branches\n       \tare considered, e.g. `git branch --delete-merged 'origin/*'\n       \t'topic-*'`.\n     @@ builtin/branch.c\n      +#include \"diff.h\"\n      +#include \"diffcore.h\"\n      +#include \"hex.h\"\n     -+#include \"merge-ll.h\"\n      +#include \"revision.h\"\n      +#include \"tree-walk.h\"\n     -+#include \"xdiff-interface.h\"\n       \n       static const char * const builtin_branch_usage[] = {\n       \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n       \treturn ret;\n       }\n       \n     -+enum change_state {\n     -+\tCHANGE_MISSING,\n     -+\tCHANGE_LANDED,\n     -+\tCHANGE_NEEDS_MERGE,\n     -+};\n     -+\n      +struct branch_change {\n      +\tchar *path;\n     -+\tstruct object_id base_oid, branch_oid, upstream_oid;\n     ++\tstruct object_id branch_oid;\n      +\tunsigned short branch_mode;\n     -+\tenum change_state state;\n      +};\n      +\n      +struct landing_search {\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\t\t\t   search->changes_alloc);\n      +\t\tchange = &search->changes[search->changes_nr++];\n      +\t\tchange->path = xstrdup(p->two->path);\n     -+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n     -+\t\t       &p->one->oid : null_oid(the_hash_algo));\n      +\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n      +\t\t       &p->two->oid : null_oid(the_hash_algo));\n      +\t\tchange->branch_mode = p->two->mode;\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\tdiff_flush(&opt);\n      +}\n      +\n     -+static int merge_keeps_upstream(const struct branch_change *change)\n     -+{\n     -+\tmmfile_t base, upstream, branch;\n     -+\tmmbuffer_t result = { 0 };\n     -+\tint ret;\n     -+\n     -+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n     -+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n     -+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n     -+\tret = ll_merge(&result, change->path, &base, \"base\",\n     -+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n     -+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n     -+\t      result.size == upstream.size &&\n     -+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n     -+\n     -+\tfree(base.ptr);\n     -+\tfree(upstream.ptr);\n     -+\tfree(branch.ptr);\n     -+\tfree(result.ptr);\n     -+\treturn ret;\n     -+}\n     -+\n     -+static enum change_state change_state_at(struct branch_change *change,\n     -+\t\t\t\t\t struct commit *commit)\n     ++static int change_landed(const struct branch_change *change,\n     ++\t\t\t struct commit *commit)\n      +{\n     ++\tstruct object_id oid;\n      +\tunsigned short mode;\n      +\n      +\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n     -+\t\t\t   change->path, &change->upstream_oid, &mode))\n     -+\t\treturn is_null_oid(&change->branch_oid) ?\n     -+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n     -+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n     -+\t\treturn mode == change->branch_mode ?\n     -+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n     -+\tif (is_null_oid(&change->base_oid) ||\n     -+\t    is_null_oid(&change->branch_oid) ||\n     -+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n     -+\t    mode != change->branch_mode || !S_ISREG(mode))\n     -+\t\treturn CHANGE_MISSING;\n     -+\treturn CHANGE_NEEDS_MERGE;\n     ++\t\t\t   change->path, &oid, &mode))\n     ++\t\treturn is_null_oid(&change->branch_oid);\n     ++\treturn oideq(&oid, &change->branch_oid) &&\n     ++\t       mode == change->branch_mode;\n      +}\n      +\n      +static int changes_landed(struct landing_search *search,\n      +\t\t\t  struct commit *commit)\n      +{\n     -+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     -+\t\tstruct branch_change *change = &search->changes[i];\n     -+\n     -+\t\tchange->state = change_state_at(change, commit);\n     -+\t\tif (change->state == CHANGE_MISSING)\n     -+\t\t\treturn 0;\n     -+\t}\n     -+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     -+\t\tstruct branch_change *change = &search->changes[i];\n     -+\n     -+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n     -+\t\t    !merge_keeps_upstream(change))\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++)\n     ++\t\tif (!change_landed(&search->changes[i], commit))\n      +\t\t\treturn 0;\n     -+\t}\n      +\treturn 1;\n      +}\n      +\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\tdiff_free(&opt);\n      +}\n      +\n     -+static int branch_opted_out(struct strbuf *key, const char *branch_name,\n     -+\t\t\t    unsigned int flags)\n     -+{\n     -+\tint opt_out;\n     -+\n     -+\tstrbuf_reset(key);\n     -+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n     -+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n     -+\t    opt_out)\n     -+\t\treturn 0;\n     -+\tif (!(flags & DELETE_BRANCH_QUIET))\n     -+\t\tfprintf(stderr,\n     -+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n     -+\t\t\tbranch_name, branch_name);\n     -+\treturn 1;\n     -+}\n     -+\n     - static int delete_merged_branches(const struct strvec *upstreams,\n     - \t\t\t\t const char **argv, unsigned int flags)\n     + static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n     + \t\t\t     unsigned int flags)\n       {\n      @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n       \tstruct ref_array candidates = { 0 };\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \tstruct strvec branches_to_delete = STRVEC_INIT;\n       \tstruct strbuf key = STRBUF_INIT;\n       \tstruct hashmap_iter iter;\n     -@@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n     - \t\tconst char *branch_name;\n     - \t\tstruct branch *branch;\n     - \t\tconst char *upstream_refname;\n     --\t\tint opt_out;\n     - \n     - \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n     - \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n      @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n       \t\t\tcontinue;\n       \t\tif (check_branch_commit(branch_name, branch_name,\n       \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n      -\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n     --\t\t\tcontinue;\n     --\n     --\t\tstrbuf_reset(&key);\n     --\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n     --\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n     --\t\t    !opt_out) {\n     --\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n     --\t\t\t\tfprintf(stderr,\n     --\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n     --\t\t\t\t\tbranch_name, branch_name);\n      +\t\t\t\t\tFILTER_REFS_BRANCHES,\n      +\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n      +\t\t\tstruct commit *rev = lookup_commit_reference(\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n      +\t\t\t\t.upstream = upstream,\n      +\t\t\t};\n       \t\t\tcontinue;\n     - \t\t}\n     ++\t\t}\n     + \n     + \t\tif (!branch_opted_out(&key, branch_name, flags))\n     + \t\t\tstrset_add(&deletable_branch_names, branch_name);\n     + \t}\n       \n     --\t\tstrset_add(&deletable_branch_names, branch_name);\n     -+\t\tif (!branch_opted_out(&key, branch_name, flags))\n     -+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n     -+\t}\n     -+\n      +\tfind_landed_commits(searches, searches_nr, &landed_commits);\n      +\tfor (size_t i = 0; i < searches_nr; i++) {\n      +\t\tconst char *branch_name = searches[i].branch_name;\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n      +\t\tif (strmap_contains(&landed_commits, branch_name) &&\n      +\t\t    !branch_opted_out(&key, branch_name, flags))\n      +\t\t\tstrset_add(&deletable_branch_names, branch_name);\n     - \t}\n     - \n     ++\t}\n     ++\n       \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n     + \t\t\t\t     &protected_branch_names);\n     + \n      @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n       \t\t\t\t      FILTER_REFS_BRANCHES,\n       \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +}\n      +\n     ++rebase_merge_upstream () {\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit rebase next \"$1\" &&\n     ++\t\tgit checkout next &&\n     ++\t\tgit merge --ff-only \"$1\" &&\n     ++\t\tgit checkout main\n     ++\t)\n     ++}\n     ++\n     ++test_expect_success '--delete-merged deletes a rebase merged branch' '\n     ++\tsetup_repo_for_delete_merged &&\n     ++\tpush_topic rebased rebased-one rebased-two &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_commit before-rebase &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\trebase_merge_upstream rebased &&\n     ++\tlanded=$(git -C upstream rev-parse --short next) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\t\tsha=$(git rev-parse --short rebased) &&\n     ++\n     ++\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n     ++\t\ttest_cmp expect actual &&\n     ++\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\tmain\n     ++\t\tEOF\n     ++\t)\n     ++'\n     ++\n      +test_expect_success '--delete-merged deletes a squash merged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic squashed squashed-one squashed-two &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +'\n      +\n     -+test_expect_success '--delete-merged deletes a squash merged branch after upstream changed the same file' '\n     -+\t(\n     -+\t\tcd upstream &&\n     -+\t\tgit checkout next &&\n     -+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n     -+\t\tgit add shared &&\n     -+\t\tgit commit -m \"add shared\" &&\n     -+\t\tgit checkout main\n     -+\t) &&\n     -+\tsetup_repo_for_delete_merged &&\n     -+\t(\n     -+\t\tcd repo &&\n     -+\t\tgit checkout -b edits-shared --track origin/next &&\n     -+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n     -+\t\tgit commit -a -m \"edit line 2\" &&\n     -+\t\tgit push origin edits-shared &&\n     -+\t\tgit checkout --detach\n     -+\t) &&\n     -+\t(\n     -+\t\tcd upstream &&\n     -+\t\tgit checkout next &&\n     -+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n     -+\t\tgit commit -a -m \"edit line 5\" &&\n     -+\t\tgit checkout main\n     -+\t) &&\n     -+\tsquash_merge_upstream edits-shared &&\n     -+\tsquash=$(git -C upstream rev-parse --short next) &&\n     -+\t(\n     -+\t\tcd repo &&\n     -+\t\tgit fetch origin &&\n     -+\t\tsha=$(git rev-parse --short edits-shared) &&\n     -+\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     -+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n     -+\t\ttest_cmp expect actual &&\n     -+\n     -+\t\tcheck_branches <<-\\EOF\n     -+\t\tmain\n     -+\t\tEOF\n     -+\t)\n     -+'\n     -+\n      +test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic a-unmerged unmerged-work &&\n 2:  ec88bde252 ! 3:  2befb182dc branch: add --no-squashed to skip the squash search\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    branch: add --no-squashed to skip the squash search\n     +    branch: add --squashed to find squash merged branches\n      \n     -    Finding branches that were squash or rebase merged makes \"git branch\n     -    --delete-merged\" slower, and in a partial clone it may have to\n     -    download blobs. Users who do not need it have no way to skip that\n     -    search.\n     +    A branch that upstream integrated with\n      \n     -    Add --no-squashed, which only deletes branches whose tip is reachable\n     -    from their upstream.\n     +        git merge --squash topic\n     +        git commit\n     +\n     +    after upstream had also changed the files the branch touches is still\n     +    kept by \"git branch --delete-merged\". The squash commit has neither the\n     +    branch's version of those files nor the fork point's, so the previous\n     +    check cannot see that the work landed. This happens all the time on\n     +    hosting services, where the \"Squash and merge\" button on GitHub lands\n     +    pull requests this way while the main branch keeps moving. A rebase\n     +    merge onto such changes looks the same.\n     +\n     +    Add --squashed, which for each such file merges the branch's version\n     +    into the upstream one and treats the change as landed when that leaves\n     +    the upstream version unchanged. This has to read the files, so it is\n     +    slower, and in a partial clone it may download blobs. It is therefore\n     +    only done when asked for.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/git-branch.adoc: git branch (-m|-M) [<old-branch>] <new-branch>\n       git branch (-d|-D) [-r] <branch-name>...\n       git branch --edit-description [<branch-name>]\n      -git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n     -+git branch [--dry-run] [--no-squashed] (--delete-merged <pattern>)... [<branch-pattern>...]\n     ++git branch [--dry-run] [--squashed] (--delete-merged <pattern>)... [<branch-pattern>...]\n       \n       DESCRIPTION\n       -----------\n     @@ Documentation/git-branch.adoc: remove it anyway.\n       \tsanity-checking a wide pattern like `'origin/*'` before\n       \tcommitting to the deletion.\n       \n     -+`--no-squashed`::\n     -+\tWith `--delete-merged`, only delete branches whose tip is\n     -+\treachable from their upstream. This skips the search for\n     -+\tbranches that were squash or rebase merged, which takes longer\n     -+\tand may have to download blobs in a partial clone.\n     ++`--squashed`::\n     ++\tWith `--delete-merged`, also delete a branch that was squash or\n     ++\trebase merged after upstream changed the same files. Upstream\n     ++\tthen has neither the branch's version of those files nor the fork\n     ++\tpoint's, so the branch's version is merged into upstream's, and\n     ++\tthe file counts as landed when that leaves upstream's version\n     ++\tunchanged. This reads those files, so it takes longer and may\n     ++\tdownload blobs in a partial clone.\n      +\n       `-v`::\n       `-vv`::\n       `--verbose`::\n      \n       ## builtin/branch.c ##\n     +@@\n     + #include \"diff.h\"\n     + #include \"diffcore.h\"\n     + #include \"hex.h\"\n     ++#include \"merge-ll.h\"\n     + #include \"revision.h\"\n     + #include \"tree-walk.h\"\n     ++#include \"xdiff-interface.h\"\n     + \n     + static const char * const builtin_branch_usage[] = {\n     + \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n      @@ builtin/branch.c: enum delete_branch_flags {\n       \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n       \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n       \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n     -+\tDELETE_BRANCH_NO_SQUASHED = (1 << 5),\n     ++\tDELETE_BRANCH_SQUASHED = (1 << 5),\n       };\n       \n       static int check_branch_commit(const char *branchname, const char *refname,\n     +@@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n     + \treturn ret;\n     + }\n     + \n     ++enum change_state {\n     ++\tCHANGE_MISSING,\n     ++\tCHANGE_LANDED,\n     ++\tCHANGE_NEEDS_MERGE,\n     ++};\n     ++\n     + struct branch_change {\n     + \tchar *path;\n     +-\tstruct object_id branch_oid;\n     ++\tstruct object_id base_oid, branch_oid, upstream_oid;\n     + \tunsigned short branch_mode;\n     ++\tenum change_state state;\n     + };\n     + \n     + struct landing_search {\n     +@@ builtin/branch.c: static void collect_branch_changes(struct landing_search *search,\n     + \t\t\t   search->changes_alloc);\n     + \t\tchange = &search->changes[search->changes_nr++];\n     + \t\tchange->path = xstrdup(p->two->path);\n     ++\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n     ++\t\t       &p->one->oid : null_oid(the_hash_algo));\n     + \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n     + \t\t       &p->two->oid : null_oid(the_hash_algo));\n     + \t\tchange->branch_mode = p->two->mode;\n     +@@ builtin/branch.c: static void collect_branch_changes(struct landing_search *search,\n     + \tdiff_flush(&opt);\n     + }\n     + \n     +-static int change_landed(const struct branch_change *change,\n     +-\t\t\t struct commit *commit)\n     ++static int merge_keeps_upstream(const struct branch_change *change)\n     ++{\n     ++\tmmfile_t base, upstream, branch;\n     ++\tmmfile_t result = { 0 };\n     ++\tint ret;\n     ++\n     ++\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n     ++\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n     ++\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n     ++\tret = ll_merge(&result, change->path, &base, \"base\",\n     ++\t\t       &upstream, \"upstream\", &branch, \"branch\",\n     ++\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n     ++\t      result.size == upstream.size &&\n     ++\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n     ++\n     ++\tfree(base.ptr);\n     ++\tfree(upstream.ptr);\n     ++\tfree(branch.ptr);\n     ++\tfree(result.ptr);\n     ++\treturn ret;\n     ++}\n     ++\n     ++static enum change_state change_state_at(struct branch_change *change,\n     ++\t\t\t\t\t struct commit *commit)\n     + {\n     +-\tstruct object_id oid;\n     + \tunsigned short mode;\n     + \n     + \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n     +-\t\t\t   change->path, &oid, &mode))\n     +-\t\treturn is_null_oid(&change->branch_oid);\n     +-\treturn oideq(&oid, &change->branch_oid) &&\n     +-\t       mode == change->branch_mode;\n     ++\t\t\t   change->path, &change->upstream_oid, &mode))\n     ++\t\treturn is_null_oid(&change->branch_oid) ?\n     ++\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n     ++\tif (oideq(&change->upstream_oid, &change->branch_oid))\n     ++\t\treturn mode == change->branch_mode ?\n     ++\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n     ++\tif (is_null_oid(&change->base_oid) ||\n     ++\t    is_null_oid(&change->branch_oid) ||\n     ++\t    oideq(&change->upstream_oid, &change->base_oid) ||\n     ++\t    mode != change->branch_mode || !S_ISREG(mode))\n     ++\t\treturn CHANGE_MISSING;\n     ++\treturn CHANGE_NEEDS_MERGE;\n     + }\n     + \n     + static int changes_landed(struct landing_search *search,\n     +-\t\t\t  struct commit *commit)\n     ++\t\t\t  struct commit *commit, unsigned int flags)\n     + {\n     +-\tfor (size_t i = 0; i < search->changes_nr; i++)\n     +-\t\tif (!change_landed(&search->changes[i], commit))\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     ++\t\tstruct branch_change *change = &search->changes[i];\n     ++\n     ++\t\tchange->state = change_state_at(change, commit);\n     ++\t\tif (change->state == CHANGE_MISSING ||\n     ++\t\t    (change->state == CHANGE_NEEDS_MERGE &&\n     ++\t\t     !(flags & DELETE_BRANCH_SQUASHED)))\n     + \t\t\treturn 0;\n     ++\t}\n     ++\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     ++\t\tstruct branch_change *change = &search->changes[i];\n     ++\n     ++\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n     ++\t\t    !merge_keeps_upstream(change))\n     ++\t\t\treturn 0;\n     ++\t}\n     + \treturn 1;\n     + }\n     + \n     +@@ builtin/branch.c: static int touches_changes(struct string_list *paths,\n     + \n     + static struct commit *find_landed_commit(struct landing_search *search,\n     + \t\t\t\t\t struct changed_paths *cache,\n     +-\t\t\t\t\t struct diff_options *opt)\n     ++\t\t\t\t\t struct diff_options *opt,\n     ++\t\t\t\t\t unsigned int flags)\n     + {\n     + \tstruct commit *commit, *landed = NULL;\n     + \tstruct strvec args = STRVEC_INIT;\n     +@@ builtin/branch.c: static struct commit *find_landed_commit(struct landing_search *search,\n     + \twhile (!landed && (commit = get_revision(&revs)))\n     + \t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n     + \t\t\t\t    search) &&\n     +-\t\t    changes_landed(search, commit))\n     ++\t\t    changes_landed(search, commit, flags))\n     + \t\t\tlanded = commit;\n     + \trelease_revisions(&revs);\n     + \tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n     +@@ builtin/branch.c: static struct commit *find_landed_commit(struct landing_search *search,\n     + \n     + static void find_landed_commits(struct landing_search *searches,\n     + \t\t\t\tsize_t searches_nr,\n     +-\t\t\t\tstruct strmap *landed_commits)\n     ++\t\t\t\tstruct strmap *landed_commits,\n     ++\t\t\t\tunsigned int flags)\n     + {\n     + \tstruct changed_paths cache;\n     + \tstruct diff_options opt;\n     +@@ builtin/branch.c: static void find_landed_commits(struct landing_search *searches,\n     + \n     + \t\tif (!searches[i].changes_nr)\n     + \t\t\tcontinue;\n     +-\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n     ++\t\tlanded = find_landed_commit(&searches[i], &cache, &opt, flags);\n     + \t\tif (landed)\n     + \t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n     + \t\t\t\t   landed);\n      @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n     - \t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n     - \t\t\t\tupstream_refname);\n     - \n     --\t\t\tif (!rev || !upstream)\n     -+\t\t\tif ((flags & DELETE_BRANCH_NO_SQUASHED) ||\n     -+\t\t\t    !rev || !upstream)\n     - \t\t\t\tcontinue;\n     - \t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n     - \t\t\tsearches[searches_nr++] = (struct landing_search) {\n     + \t\t\tstrset_add(&deletable_branch_names, branch_name);\n     + \t}\n     + \n     +-\tfind_landed_commits(searches, searches_nr, &landed_commits);\n     ++\tfind_landed_commits(searches, searches_nr, &landed_commits, flags);\n     + \tfor (size_t i = 0; i < searches_nr; i++) {\n     + \t\tconst char *branch_name = searches[i].branch_name;\n     + \n      @@ builtin/branch.c: int cmd_branch(int argc,\n       \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n       \tstruct strvec delete_merged = STRVEC_INIT;\n       \tint dry_run = 0;\n     -+\tint no_squashed = 0;\n     ++\tint squashed = 0;\n       \tconst char *new_upstream = NULL;\n       \tint noncreate_actions = 0;\n       \t/* possible options */\n     @@ builtin/branch.c: int cmd_branch(int argc,\n       \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n       \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n       \t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n     -+\t\tOPT_BOOL(0, \"no-squashed\", &no_squashed,\n     -+\t\t\tN_(\"with --delete-merged, skip the search for squash merged branches\")),\n     ++\t\tOPT_BOOL(0, \"squashed\", &squashed,\n     ++\t\t\tN_(\"with --delete-merged, also find branches merged after upstream changed their files\")),\n       \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n       \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n       \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n     @@ builtin/branch.c: int cmd_branch(int argc,\n       \n       \tif (dry_run && !delete_merged.nr)\n       \t\tdie(_(\"--dry-run requires --delete-merged\"));\n     -+\tif (no_squashed && !delete_merged.nr)\n     -+\t\tdie(_(\"--no-squashed requires --delete-merged\"));\n     ++\tif (squashed && !delete_merged.nr)\n     ++\t\tdie(_(\"--squashed requires --delete-merged\"));\n       \n       \tif (recurse_submodules_explicit) {\n       \t\tif (!submodule_propagate_branches)\n     @@ builtin/branch.c: int cmd_branch(int argc,\n       \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n      -\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n      +\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n     -+\t\t\t\t\t     (no_squashed ? DELETE_BRANCH_NO_SQUASHED : 0));\n     ++\t\t\t\t\t     (squashed ? DELETE_BRANCH_SQUASHED : 0));\n       \t\tgoto out;\n       \t} else if (show_current) {\n       \t\tprint_current_branch_name();\n      \n       ## t/t3200-branch.sh ##\n     -@@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes a squash merged branch after upstre\n     +@@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes a squash merged branch that was rev\n       \t)\n       '\n       \n     -+test_expect_success '--delete-merged --no-squashed deletes only reachable branches' '\n     ++test_expect_success '--delete-merged --squashed deletes a squash merged branch after upstream changed the same file' '\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n     ++\t\tgit add shared &&\n     ++\t\tgit commit -m \"add shared\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n      +\tsetup_repo_for_delete_merged &&\n     -+\tcreate_merged_branch merged &&\n     -+\tpush_topic squash-merged squash-merged-work &&\n     -+\tsquash_merge_upstream squash-merged &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit checkout -b edits-shared --track origin/next &&\n     ++\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n     ++\t\tgit commit -a -m \"edit line 2\" &&\n     ++\t\tgit push origin edits-shared &&\n     ++\t\tgit checkout --detach\n     ++\t) &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n     ++\t\tgit commit -a -m \"edit line 5\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\tsquash_merge_upstream edits-shared &&\n     ++\tsquash=$(git -C upstream rev-parse --short next) &&\n      +\t(\n      +\t\tcd repo &&\n      +\t\tgit fetch origin &&\n     -+\t\tsha=$(git rev-parse --short merged) &&\n     ++\t\tsha=$(git rev-parse --short edits-shared) &&\n     ++\n     ++\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\ttest_must_be_empty actual &&\n     ++\t\tcheck_branches <<-\\EOF &&\n     ++\t\tedits-shared\n     ++\t\tmain\n     ++\t\tEOF\n      +\n     -+\t\tgit branch --delete-merged origin/next --no-squashed \\\n     ++\t\tgit branch --delete-merged origin/next --squashed \\\n      +\t\t\t>actual 2>&1 &&\n     -+\t\techo \"Deleted branch merged (was $sha).\" >expect &&\n     ++\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n      +\t\tcheck_branches <<-\\EOF\n      +\t\tmain\n     -+\t\tsquash-merged\n      +\t\tEOF\n      +\t)\n      +'\n     @@ t/t3200-branch.sh: test_expect_success \"branch -d still deletes a deleteMerged=f\n       \t)\n       '\n       \n     -+test_expect_success '--no-squashed without --delete-merged is rejected' '\n     -+\ttest_must_fail git -C forked branch --no-squashed 2>err &&\n     ++test_expect_success '--squashed without --delete-merged is rejected' '\n     ++\ttest_must_fail git -C forked branch --squashed 2>err &&\n      +\ttest_grep \"requires --delete-merged\" err\n      +'\n      +\n\n-- \ngitgitgadget\n\n"},{"id":"555730","messageId":"82413e5cd762f342358525cd7891ef3c5fa1a8e0.1792585717.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"[PATCH v3 1/3] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T12:28:35Z","receivedAt":"2026-10-21T12:28:35Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe next commit lets \"git branch --delete-merged\" delete branches whose\ntips are not reachable from their upstream, and those must honor\nbranch.<name>.deleteMerged=false as well. Move the check into a helper\nso it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..171b13b081 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"555731","messageId":"9a7e88ced6600a8d6331ca9c4bcdc6cd208dd73f.1792585717.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"[PATCH v3 2/3] branch: let --delete-merged find rebase merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T12:28:36Z","receivedAt":"2026-10-21T12:28:36Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" button on GitHub does. The upstream holds rewritten\ncopies of the branch's commits, so its tip is not reachable from it and\nit looks unmerged forever.\n\nTreat such a branch as merged when some upstream commit since the fork\npoint has the branch's version of every file the branch changed. Name\nthat commit in the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first upstream commit that has them is used, so the branch is\ndeleted even if upstream later reverted or reworked them. Nothing is\nlost, since that commit keeps them in the upstream history. A branch\nwhose changes only partly landed is kept.\n\nThe same check finds a squash merge when upstream did not change the\nsame files between the fork point and the squash. It never reads file\ncontents, so it stays cheap in a partial clone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc |  15 +-\n builtin/branch.c              | 251 +++++++++++++++++++++++++++++++++-\n t/t3200-branch.sh             | 137 +++++++++++++++++++\n 3 files changed, 391 insertions(+), 12 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..3d6549b977 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -204,12 +204,15 @@ This option is only applicable in non-verbose mode.\n \n `--delete-merged <pattern>`::\n \tDelete local branches whose configured upstream matches\n-\t_<pattern>_, but only when their tip is reachable from that\n-\tupstream. In other words, the work on the branch has already\n-\tlanded on the upstream it tracks, so the local copy is no longer\n-\tneeded. _<pattern>_ may name a ref, a remote (using the branch its\n-\t`HEAD` points at), or a shell-style glob. The option can be\n-\trepeated to widen the upstream match.\n+\t_<pattern>_, but only when their work has already landed on that\n+\tupstream, so the local copy is no longer needed. This is the case\n+\twhen their tip is reachable from the upstream, or when some\n+\tupstream commit has their version of every file they changed, as\n+\thappens after a rebase merge, even if those changes were later\n+\treverted. The message for such a branch names the first upstream\n+\tcommit that has them. _<pattern>_ may name a ref, a remote (using\n+\tthe branch its `HEAD` points at), or a shell-style glob. The\n+\toption can be repeated to widen the upstream match.\n \tOptional _<branch-pattern>_ arguments limit which local branches\n \tare considered, e.g. `git branch --delete-merged 'origin/*'\n \t'topic-*'`.\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 171b13b081..2a2e370401 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -236,7 +242,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +340,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +365,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -824,6 +850,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options opt;\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&opt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&opt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *opt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(opt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *opt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options opt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\topt.no_free = 1;\n+\tdiff_setup_done(&opt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\topt.no_free = 0;\n+\tdiff_free(&opt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -849,6 +1054,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -883,13 +1091,37 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n+\t\t\t\tupstream_refname);\n+\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -901,7 +1133,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -909,6 +1141,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1141,7 +1380,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..5ba19a34e2 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1979,6 +1979,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-merged deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n-- \ngitgitgadget\n\n\n"},{"id":"555732","messageId":"2befb182dc7c551d0d6ae0d8803fe717b7b58c61.1792585717.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"[PATCH v3 3/3] branch: add --squashed to find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T12:28:37Z","receivedAt":"2026-10-21T12:28:37Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-merged\". The squash commit has neither the\nbranch's version of those files nor the fork point's, so the previous\ncheck cannot see that the work landed. This happens all the time on\nhosting services, where the \"Squash and merge\" button on GitHub lands\npull requests this way while the main branch keeps moving. A rebase\nmerge onto such changes looks the same.\n\nAdd --squashed, which for each such file merges the branch's version\ninto the upstream one and treats the change as landed when that leaves\nthe upstream version unchanged. This has to read the files, so it is\nslower, and in a partial clone it may download blobs. It is therefore\nonly done when asked for.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 11 +++-\n builtin/branch.c              | 96 ++++++++++++++++++++++++++++-------\n t/t3200-branch.sh             | 55 ++++++++++++++++++++\n 3 files changed, 144 insertions(+), 18 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 3d6549b977..3b414be171 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -25,7 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch>\n git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n-git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] [--squashed] (--delete-merged <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -244,6 +244,15 @@ remove it anyway.\n \tsanity-checking a wide pattern like `'origin/*'` before\n \tcommitting to the deletion.\n \n+`--squashed`::\n+\tWith `--delete-merged`, also delete a branch that was squash or\n+\trebase merged after upstream changed the same files. Upstream\n+\tthen has neither the branch's version of those files nor the fork\n+\tpoint's, so the branch's version is merged into upstream's, and\n+\tthe file counts as landed when that leaves upstream's version\n+\tunchanged. This reads those files, so it takes longer and may\n+\tdownload blobs in a partial clone.\n+\n `-v`::\n `-vv`::\n `--verbose`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 2a2e370401..9ac4dcb162 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -207,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_SQUASHED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -850,10 +853,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -885,6 +895,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -892,25 +904,67 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&opt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n-\t\t\t  struct commit *commit)\n+\t\t\t  struct commit *commit, unsigned int flags)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING ||\n+\t\t    (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t     !(flags & DELETE_BRANCH_SQUASHED)))\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \n@@ -961,7 +1015,8 @@ static int touches_changes(struct string_list *paths,\n \n static struct commit *find_landed_commit(struct landing_search *search,\n \t\t\t\t\t struct changed_paths *cache,\n-\t\t\t\t\t struct diff_options *opt)\n+\t\t\t\t\t struct diff_options *opt,\n+\t\t\t\t\t unsigned int flags)\n {\n \tstruct commit *commit, *landed = NULL;\n \tstruct strvec args = STRVEC_INIT;\n@@ -978,7 +1033,7 @@ static struct commit *find_landed_commit(struct landing_search *search,\n \twhile (!landed && (commit = get_revision(&revs)))\n \t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n \t\t\t\t    search) &&\n-\t\t    changes_landed(search, commit))\n+\t\t    changes_landed(search, commit, flags))\n \t\t\tlanded = commit;\n \trelease_revisions(&revs);\n \tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n@@ -989,7 +1044,8 @@ static struct commit *find_landed_commit(struct landing_search *search,\n \n static void find_landed_commits(struct landing_search *searches,\n \t\t\t\tsize_t searches_nr,\n-\t\t\t\tstruct strmap *landed_commits)\n+\t\t\t\tstruct strmap *landed_commits,\n+\t\t\t\tunsigned int flags)\n {\n \tstruct changed_paths cache;\n \tstruct diff_options opt;\n@@ -1018,7 +1074,7 @@ static void find_landed_commits(struct landing_search *searches,\n \n \t\tif (!searches[i].changes_nr)\n \t\t\tcontinue;\n-\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt, flags);\n \t\tif (landed)\n \t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n \t\t\t\t   landed);\n@@ -1113,7 +1169,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n-\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfind_landed_commits(searches, searches_nr, &landed_commits, flags);\n \tfor (size_t i = 0; i < searches_nr; i++) {\n \t\tconst char *branch_name = searches[i].branch_name;\n \n@@ -1221,6 +1277,7 @@ int cmd_branch(int argc,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n \tint dry_run = 0;\n+\tint squashed = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n \t/* possible options */\n@@ -1279,6 +1336,8 @@ int cmd_branch(int argc,\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n \t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\tOPT_BOOL(0, \"squashed\", &squashed,\n+\t\t\tN_(\"with --delete-merged, also find branches merged after upstream changed their files\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1343,6 +1402,8 @@ int cmd_branch(int argc,\n \n \tif (dry_run && !delete_merged.nr)\n \t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (squashed && !delete_merged.nr)\n+\t\tdie(_(\"--squashed requires --delete-merged\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1385,7 +1446,8 @@ int cmd_branch(int argc,\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n-\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     (squashed ? DELETE_BRANCH_SQUASHED : 0));\n \t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 5ba19a34e2..f3980106fa 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2089,6 +2089,56 @@ test_expect_success '--delete-merged deletes a squash merged branch that was rev\n \t)\n '\n \n+test_expect_success '--delete-merged --squashed deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF &&\n+\t\tedits-shared\n+\t\tmain\n+\t\tEOF\n+\n+\t\tgit branch --delete-merged origin/next --squashed \\\n+\t\t\t>actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n@@ -2310,6 +2360,11 @@ test_expect_success \"branch -d still deletes a deleteMerged=false branch\" '\n \t)\n '\n \n+test_expect_success '--squashed without --delete-merged is rejected' '\n+\ttest_must_fail git -C forked branch --squashed 2>err &&\n+\ttest_grep \"requires --delete-merged\" err\n+'\n+\n test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_must_fail git -C forked branch --dry-run 2>err &&\n \ttest_grep \"requires --delete-merged\" err\n-- \ngitgitgadget\n\n"},{"id":"555763","messageId":"20261021173503.70887-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-10-21T17:35:03Z","receivedAt":"2026-10-21T17:35:03Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n>  * By default a branch counts as merged when an upstream commit has its\n>    exact version of every file it changed. This never reads file contents,\n>    so it stays cheap in a partial clone. It finds rebase merges, and squash\n>    merges where upstream left those files alone.\n>  * Replace --no-squashed with an opt-in --squashed, which also finds\n>    branches merged after upstream changed the same files by merging those\n>    files.\n\nThanks for the new round. I ran v3 (2befb182dc) on the same generated\nrepositories as v2, and added one case for a squash that landed after\nupstream changed another line of the same file. t3200 passes, and the\nbuild is clean with DEVELOPER=1.\n\nThe results match the cover letter. Without --squashed, v3 deletes the\nsquash merges, the GitHub-style squash, the rebase merge, the rename,\nand the squash that was later reverted. It keeps the new case.\n--squashed also deletes the new case, and \"landed as\" names the correct\ncommit each time. A branch whose work only partly landed is kept, with\nor without --squashed.\n\nBut the false positive from my v2 report is still there. A branch that\nwas never merged is deleted because another author made the same change\nin an unrelated commit. In v2 I could turn that off with --no-squashed.\nIn v3 the cheap check finds it, so it happens with no option at all.\n\nThat is the case Junio called \"irrelevant\", and he suggested that\n\"--delete-merged\" stay the strict \"my commits are in upstream\" check.\nThe file contents of such a branch are in upstream, but its own commits\nand messages are not, and the user did not ask for the looser meaning.\n\nSo I would still prefer the whole looser check behind one opt-in, with\na config variable for the default. The cheap check and --squashed could\nthen be two levels of that one option, for example:\n\n    git branch --delete-merged origin/main --landed=files\n    git branch --delete-merged origin/main --landed=merge\n\nUsers who land everything through \"Squash and merge\" set it once in the\nconfig, and plain --delete-merged keeps its current meaning.\n\nA branch checked out in a worktree is still skipped with no message,\nwhile a branch with deleteMerged=false prints \"Skipping\". I sent an RFC\nfor \"git worktree list --merged\" about that case:\n\n  https://lore.kernel.org/git/20261021173350.67854-1-me@jaspermayone.com/\n\nThanks,\nJasper\n\n"},{"id":"555779","messageId":"CAHwyqnWnp0W17TXbXgLQPaGuwasqndgoPHV1CL3mBpfGFYadtw@mail.gmail.com","threadId":"66417","inReplyTo":"20261021173503.70887-1-me@jaspermayone.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-21T18:51:43Z","receivedAt":"2026-10-21T18:51:43Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> But the false positive from my v2 report is still there. A branch that\n> was never merged is deleted because another author made the same change\n> in an unrelated commit. In v2 I could turn that off with --no-squashed.\n> In v3 the cheap check finds it, so it happens with no option at all.\n>\n> That is the case Junio called \"irrelevant\", and he suggested that\n> \"--delete-merged\" stay the strict \"my commits are in upstream\" check.\n> The file contents of such a branch are in upstream, but its own commits\n> and messages are not, and the user did not ask for the looser meaning.\n\nMy sincere apology for skipping over your post before.\n\nMy branch being pruned because someone else did the same thing would\nindeed feel weird. And it could be stopped by doing an author check.\n\nBut I question if it’s realistic at all. The code has to be exactly\nthe same, and I don’t think I have ever seen that happen, I would call\nthis an extremely rare edge case.\n\nAnd if it did happen and it was *incorrectly* pruned, it is still not\ncompletely lost, because by definition it is in the upstream already.\nSo why would you actually need that branch? (But it won’t happen)\n\n> So I would still prefer the whole looser check behind one opt-in, with\n> a config variable for the default. The cheap check and --squashed could\n> then be two levels of that one option, for example:\n>\n>     git branch --delete-merged origin/main --landed=files\n>     git branch --delete-merged origin/main --landed=merge\n>\n> Users who land everything through \"Squash and merge\" set it once in the\n> config, and plain --delete-merged keeps its current meaning.\n\nI think this is pushing it in terms of UX mental load. I will never\nrun this check without specifying the highest level of pruning. I also\nhave another topic open to be able to skip the '**' pattern.\n\nIt's about giving users the simplest-to-use tool to work with, not\nendless configurability for a command that just deletes merged\nbranches.\n\n> A branch checked out in a worktree is still skipped with no message,\n> while a branch with deleteMerged=false prints \"Skipping\". I sent an RFC\n> for \"git worktree list --merged\" about that case:\n>\n>   https://lore.kernel.org/git/20261021173350.67854-1-me@jaspermayone.com/\n\nI'll take a look at that after sending this, I haven't yet. Thanks in advance!\n\n\nHarald\n\n"},{"id":"555819","messageId":"xmqqjynajndt.fsf@gitster.g","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-22T05:49:18Z","receivedAt":"2026-10-22T05:49:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> This series finds the first upstream commit since the fork point that has\n> the branch's version of every file the branch changed, comparing only object\n> ids. With --squashed, a file that upstream also changed counts when merging\n> the branch's version into upstream's leaves it unchanged. Timed on my clone\n> with 65 branches, this takes 0.5s.\n\nIt would be very surprising for the unsuspecting users to loosen the\nbehaviour of \"--delete-merged\" like this series does.  As requested\nelsewhere [*], do not change the behaviour of the \"--delete-merged\"\nwhen no other \"please make it looser\" option requests otherwise.\n\n * https://lore.kernel.org/git/20261019044005.99247-1-me@jaspermayone.com/\n\nThe attached new test illustrates the extent of damage.  We add a\nfile on a private branch while the upstream adds two files, one of\nwhich has an identical contents to our file, under author identity\ndifferent from ours.  If we run \"--delete-merged\" in our repository,\nwe should not lose our branch that we never pushed, or asked them to\npull, or anything.  But when applied on top of this series, we do.\n\nWith the following applied on top of the series, with all changes to\nbuiltin/branch.c reverted, it passes.  Of course, later tests that\nassumes the looser criteria is used without an explicit opt-in will\nfail with your code changes reverted, but that is to be expected.\n\n t/t3200-branch.sh | 33 +++++++++++++++++++++++++++++++++\n 1 file changed, 33 insertions(+)\n\ndiff --git c/t/t3200-branch.sh w/t/t3200-branch.sh\nindex f3980106fa..adbcf937b5 100755\n--- c/t/t3200-branch.sh\n+++ w/t/t3200-branch.sh\n@@ -1925,6 +1925,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit -m \"add file and elif\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n\n"},{"id":"555830","messageId":"CAHwyqnXcdgzRnFgFhTfhQFFOgDxUUKoPf2sGViBTiXMK1aNttw@mail.gmail.com","threadId":"66417","inReplyTo":"xmqqjynajndt.fsf@gitster.g","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-22T07:18:40Z","receivedAt":"2026-10-22T07:18:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> It would be very surprising for the unsuspecting users to loosen the\n> behaviour of \"--delete-merged\" like this series does.  As requested\n> elsewhere [*], do not change the behaviour of the \"--delete-merged\"\n> when no other \"please make it looser\" option requests otherwise.\n>\n>  * https://lore.kernel.org/git/20261019044005.99247-1-me@jaspermayone.com/\n>\n> The attached new test illustrates the extent of damage.  We add a\n> file on a private branch while the upstream adds two files, one of\n> which has an identical contents to our file, under author identity\n> different from ours.  If we run \"--delete-merged\" in our repository,\n> we should not lose our branch that we never pushed, or asked them to\n> pull, or anything.  But when applied on top of this series, we do.\n\nOk.\n\nSo do we go with `--delete-irrelevant` or\n`--delete-irrelevant=squashed,rebased` to give us two levels here? I'd\nrather do the former.\n\n\nHarald\n\n"},{"id":"555863","messageId":"CALnO6CC0+t3PTVXC=TohzpqzgeOK4t9bKzGP-43uD1bbA4dPOg@mail.gmail.com","threadId":"66417","inReplyTo":"CAHwyqnWnp0W17TXbXgLQPaGuwasqndgoPHV1CL3mBpfGFYadtw@mail.gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-22T12:55:49Z","receivedAt":"2026-10-22T12:55:49Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Wed, Oct 21, 2026 at 2:52 PM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n> My branch being pruned because someone else did the same thing would\n> indeed feel weird. And it could be stopped by doing an author check.\n>\n> But I question if it’s realistic at all. The code has to be exactly\n> the same, and I don’t think I have ever seen that happen, I would call\n> this an extremely rare edge case.\n\nI have seen this happen when 2 PRs contain the same \"npm update\"\ncommits and are rebased onto main: whoever gets there first wins. If\nthe commits contained additional changes, then the later-rebased\ncommit loses some changes because they were handled prior, but (when\ndoing so via GitHub) the commit message is unchanged. I find this a\nbit confusing, tbh!\n\nBut, I will say it is rare. Perhaps not \"extremely\" :)\n\n-- \nD. Ben Knoble\n\n"},{"id":"555864","messageId":"CALnO6CCpjhGoc+MOikVqtfRr2CJV-ayuLGZhEmwLXHEGML7yAw@mail.gmail.com","threadId":"66417","inReplyTo":"pull.2425.v3.git.git.1792585717.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-22T12:59:28Z","receivedAt":"2026-10-22T12:59:28Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Wed, Oct 21, 2026 at 8:28 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\n> never deleted by git branch --delete-merged, because their tips are not\n> reachable from the upstream. This treats such a branch as merged when some\n> upstream commit contains all of its changes, and names that commit in the\n> output:\n>\n> Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n>\n>\n> This series finds the first upstream commit since the fork point that has\n> the branch's version of every file the branch changed, comparing only object\n> ids. With --squashed, a file that upstream also changed counts when merging\n> the branch's version into upstream's leaves it unchanged. Timed on my clone\n> with 65 branches, this takes 0.5s.\n\n> Harald Nordgren (3):\n>   branch: move the deleteMerged opt-out check into a helper\n>   branch: let --delete-merged find rebase merged branches\n>   branch: add --squashed to find squash merged branches\n\nI hope you'll have a moment to consider my reply in\n<https://lore.kernel.org/git/CALnO6CAsCvJRywkw0WpO1nEhL+yU2-r_4Z2AHdJch1m=NK8=TA@mail.gmail.com/T/#m5244d4a2781578b3058c4cef272a2014291ab85e>\nwrt the CLI. Perhaps a modifier in --delete-merged's value would be\nbetter than another --option that modifies how it behaves?\n\n"},{"id":"555871","messageId":"CAHwyqnXYopD8=KFGdYkaqwCdm8q548QOKnyjm7SSRUqGXkOxEA@mail.gmail.com","threadId":"66417","inReplyTo":"CALnO6CC0+t3PTVXC=TohzpqzgeOK4t9bKzGP-43uD1bbA4dPOg@mail.gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-22T14:41:20Z","receivedAt":"2026-10-22T14:41:20Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Thu, Oct 22, 2026 at 2:56 PM D. Ben Knoble <ben.knoble@gmail.com> wrote:\n>\n> On Wed, Oct 21, 2026 at 2:52 PM Harald Nordgren\n> <haraldnordgren@gmail.com> wrote:\n> > My branch being pruned because someone else did the same thing would\n> > indeed feel weird. And it could be stopped by doing an author check.\n> >\n> > But I question if it’s realistic at all. The code has to be exactly\n> > the same, and I don’t think I have ever seen that happen, I would call\n> > this an extremely rare edge case.\n>\n> I have seen this happen when 2 PRs contain the same \"npm update\"\n> commits and are rebased onto main: whoever gets there first wins. If\n> the commits contained additional changes, then the later-rebased\n> commit loses some changes because they were handled prior, but (when\n> doing so via GitHub) the commit message is unchanged. I find this a\n> bit confusing, tbh!\n>\n> But, I will say it is rare. Perhaps not \"extremely\" :)\n>\n> --\n> D. Ben Knoble\n\nGood counter example!\n\nBut it would have to be something like that then: an automated\ndeterministic script.\n\n\nHarald\n\n"},{"id":"555878","messageId":"xmqqa4o5itkc.fsf@gitster.g","threadId":"66417","inReplyTo":"CAHwyqnXcdgzRnFgFhTfhQFFOgDxUUKoPf2sGViBTiXMK1aNttw@mail.gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-22T16:33:23Z","receivedAt":"2026-10-22T16:33:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> So do we go with `--delete-irrelevant` or\n> `--delete-irrelevant=squashed,rebased` to give us two levels here? I'd\n> rather do the former.\n\nTwo comments.\n\n* Naming\n\nI would avoid a negative-sounding word like \"irrelevant\".  Your work\nis almost always relevant, at least to you.\n\nThe purpose of the new feature is to find branches that the user no\nlonger needs to keep because they have been practically merged.\nPerhaps --delete-integrated?  I do not know.  The naming matters,\nand I hope somebody watching from the sidelines can suggest a better\nalternative.\n\nFor lack of a better alternative that I can suggest, I will use\n\"--delete-foo\" for the remainder of this message.\n\n* Option value\n\nAnother point is that different kinds of detection should probably\nform subset-superset relationships, rather than a Venn diagram, to\nmake them easy to understand and useful.\n\nWhen you specify --delete-foo=squashed,rebased, do you expect that a\nbranch that would be removed by --delete-merged (i.e., merged as\nidentical commits) would be kept because it was not squash-merged\nor rebased onto the upstream?  I doubt users would want to make such\na distinction.\n\nI do not personally care about the distinction between rebasing and\nsquash-merging, but users probably do.  I suspect they expect that a\nmode catching squash-merged branches would also recognize a branch\nthat was straight cherry-picked (or rebased) upstream.\n\nPerhaps the options could be structured like this to be as easy to\nunderstand as possible:\n\n    --delete-foo=merged      Same as --delete-merged.\n    --delete-foo=rebased     Above, plus rebased.\n    --delete-foo=squashed    Above, plus squash-merged.\n\nWould this be a clearer way to present the feature to users?\n\nThanks.\n\n"},{"id":"555881","messageId":"xmqqv76thdvs.fsf@gitster.g","threadId":"66417","inReplyTo":"xmqqa4o5itkc.fsf@gitster.g","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-22T16:57:27Z","receivedAt":"2026-10-22T16:57:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> I would avoid a negative-sounding word like \"irrelevant\".  Your work\n> is almost always relevant, at least to you.\n>\n> The purpose of the new feature is to find branches that the user no\n> longer needs to keep because they have been practically merged.\n> Perhaps --delete-integrated?  I do not know.  The naming matters,\n> and I hope somebody watching from the sidelines can suggest a better\n> alternative.\n>\n> For lack of a better alternative that I can suggest, I will use\n> \"--delete-foo\" for the remainder of this message.\n\nHere is a short list of candidates for \"foo\" I came up with.\n\n    merged\n    integrated\n    subsumed\n    absorbed\n    included\n    done\n\nReusing the existing --delete-merged option by extending it as\n--delete-merged[=<level>] is somewhat tempting.  However, we frown\nupon options with optional values because our options by default\nallow option-arguments to be given as separate arguments.  If we\ncan, I would prefer to avoid doing so.\n\nOur documentation uses \"integrated\" to describe practically merging\nyour work upstream, whether by merging, rebasing, or squash-merging.\nHowever, that word is a tad too long.  \"subsumed\", \"absorbed\", and\n\"included\" are shorter alternatives, but none of them is familiar\nin our parlance.\n\nThe word \"done\" may be a direct match to the mindset of our users.\nOnce your branch is integrated upstream and you no longer need to\nkeep pushing for its inclusion, you are \"done\" with it.\n\nI dunno.  I hope others will suggest something much better than any\nof the above ;-)\n\n"},{"id":"555885","messageId":"CAHwyqnUWab7AhqJtWkPQzw_dh+OwxiZxt2+1TTC-r+VPx+20Tg@mail.gmail.com","threadId":"66417","inReplyTo":"xmqqv76thdvs.fsf@gitster.g","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-22T17:31:44Z","receivedAt":"2026-10-22T17:31:44Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Thu, Oct 22, 2026 at 6:57 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n> > I would avoid a negative-sounding word like \"irrelevant\".  Your work\n> > is almost always relevant, at least to you.\n> >\n> > The purpose of the new feature is to find branches that the user no\n> > longer needs to keep because they have been practically merged.\n> > Perhaps --delete-integrated?  I do not know.  The naming matters,\n> > and I hope somebody watching from the sidelines can suggest a better\n> > alternative.\n> >\n> > For lack of a better alternative that I can suggest, I will use\n> > \"--delete-foo\" for the remainder of this message.\n>\n> Here is a short list of candidates for \"foo\" I came up with.\n>\n>     merged\n>     integrated\n>     subsumed\n>     absorbed\n>     included\n>     done\n\n\n> Reusing the existing --delete-merged option by extending it as\n> --delete-merged[=<level>] is somewhat tempting.  However, we frown\n> upon options with optional values because our options by default\n> allow option-arguments to be given as separate arguments.  If we\n> can, I would prefer to avoid doing so.\n\nI'm leaning toward `--delete-merged[=<level>]`. But it's good that you\nare pointing this out. It's tempting to unify it like that.\n\n> Our documentation uses \"integrated\" to describe practically merging\n> your work upstream, whether by merging, rebasing, or squash-merging.\n> However, that word is a tad too long.  \"subsumed\", \"absorbed\", and\n> \"included\" are shorter alternatives, but none of them is familiar\n> in our parlance.\n\nI like `integrated` a lot.\n\n> The word \"done\" may be a direct match to the mindset of our users.\n> Once your branch is integrated upstream and you no longer need to\n> keep pushing for its inclusion, you are \"done\" with it.\n\n`done` sounds like something else, like some script that finished. Not\nclear to me that it will be removed.\n\nWhereas `integrated` is non-ambiguous, and it's a beautiful word.\nGetting your work integrated is the goal.\n\n\n\n\nHarald\n\n"},{"id":"555888","messageId":"CAHwyqnUpheti76YJSn4h___fNZr52PzPQq0pQvA8KNyuoanxGQ@mail.gmail.com","threadId":"66417","inReplyTo":"xmqqa4o5itkc.fsf@gitster.g","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-22T17:44:08Z","receivedAt":"2026-10-22T17:44:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> I would avoid a negative-sounding word like \"irrelevant\".  Your work\n> is almost always relevant, at least to you.\n\nYes :-D\n\n> Another point is that different kinds of detection should probably\n> form subset-superset relationships, rather than a Venn diagram, to\n> make them easy to understand and useful.\n\n> When you specify --delete-foo=squashed,rebased, do you expect that a\n> branch that would be removed by --delete-merged (i.e., merged as\n> identical commits) would be kept because it was not squash-merged\n> or rebased onto the upstream?  I doubt users would want to make such\n> a distinction.\n\nVery important point regarding the design.\n\n> I do not personally care about the distinction between rebasing and\n> squash-merging, but users probably do.  I suspect they expect that a\n> mode catching squash-merged branches would also recognize a branch\n> that was straight cherry-picked (or rebased) upstream.\n>\n> Perhaps the options could be structured like this to be as easy to\n> understand as possible:\n>\n>     --delete-foo=merged      Same as --delete-merged.\n>     --delete-foo=rebased     Above, plus rebased.\n>     --delete-foo=squashed    Above, plus squash-merged.\n>\n> Would this be a clearer way to present the feature to users?\n\nYes, but we can do better on naming, something in this direction?\n\n     --delete-foo=match-hash     #Same as --delete-merged.\n     --delete-foo=match-commits  #Above, plus rebased.\n     --delete-foo=match-content  #Above, plus squash-merged.\n\nThis comes to mind\n(https://git-scm.com/docs/git-push#Documentation/git-push.txt-pushdefault),\ndifferent `push.default` options can overlap or subsume each other,\nand it's also clear that only one will be used at a time.\n\n\nHarald\n\n"},{"id":"555889","messageId":"20261022174429.81186-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"CAHwyqnUWab7AhqJtWkPQzw_dh+OwxiZxt2+1TTC-r+VPx+20Tg@mail.gmail.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-10-22T17:44:29Z","receivedAt":"2026-10-22T17:44:29Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> I'm leaning toward `--delete-merged[=<level>]`. But it's good that you\n> are pointing this out. It's tempting to unify it like that.\n\nThe value of --delete-merged is already taken by the upstream\n<pattern>, and it is required. So \"--delete-merged=squashed\" already\nmeans \"branches whose upstream is a ref called squashed\" today, and\nit cannot also carry a level without breaking that.\n\n> I like `integrated` a lot.\n\n+1 from a user who lands most work with \"Squash and merge\". It is the\nword our documentation already uses, and it does not say how the work\ngot there, which fits every level of the ladder.\n\nSince the value slot holds the pattern, the level needs its own place.\nOne shape that keeps each command line simple:\n\n    git branch --delete-merged <pattern>       # strict, as today\n    git branch --delete-integrated <pattern>   # rebased and squashed too\n\nwith a config variable if someone wants the middle step of Junio's\nladder (merged, then rebased, then squashed) as the meaning of\n--delete-integrated.\n\nOn the false positive: Ben's \"npm update\" case, Junio's new test and\nmy case are the same thing. The upstream commit has the right contents\nbut was written by someone else. The squash and rebase buttons keep\nthe author of the pull request on the upstream commit, so requiring\nthat the landing commit's author also authored a commit on the branch\nwould reject all three. Work that a maintainer applied with \"git am\"\nwould still count, because \"git am\" keeps the author too. I have not\nmeasured this, so take it as an idea to try.\n\nThanks,\nJasper\n\n"},{"id":"555892","messageId":"xmqqwlr9fwkp.fsf@gitster.g","threadId":"66417","inReplyTo":"20261022174429.81186-1-me@jaspermayone.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-22T17:56:38Z","receivedAt":"2026-10-22T17:56:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jasper Mayone <me@jaspermayone.com> writes:\n\n> Since the value slot holds the pattern, the level needs its own place.\n> One shape that keeps each command line simple:\n>\n>     git branch --delete-merged <pattern>       # strict, as today\n>     git branch --delete-integrated <pattern>   # rebased and squashed too\n>\n> with a config variable if someone wants the middle step of Junio's\n> ladder (merged, then rebased, then squashed) as the meaning of\n> --delete-integrated.\n\nI like that without configuration.  Simplicity is the ultimate\nsophistication ;-).\n\n\n"},{"id":"555978","messageId":"CAHwyqnWCndBcrxxkDTyLoC7hM3OgS8pAP=H=gt0r2ZmmKnaojg@mail.gmail.com","threadId":"66417","inReplyTo":"20261022174429.81186-1-me@jaspermayone.com","subject":"Re: [PATCH v3 0/3] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-23T09:07:01Z","receivedAt":"2026-10-23T09:07:01Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > I'm leaning toward `--delete-merged[=<level>]`. But it's good that you\n> > are pointing this out. It's tempting to unify it like that.\n>\n> The value of --delete-merged is already taken by the upstream\n> <pattern>, and it is required. So \"--delete-merged=squashed\" already\n> means \"branches whose upstream is a ref called squashed\" today, and\n> it cannot also carry a level without breaking that.\n\nThis is a good point and stops the `--delete-merged=squashed`, and\nlikely also `--delete-integrated=<level>` to keep it uniform.\n\n\nHarald\n\n"},{"id":"556275","messageId":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v4 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-26T08:33:43Z","receivedAt":"2026-10-26T08:33:43Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nThis series finds the first upstream commit since the fork point that has\nthe branch's version of every file the branch changed, comparing only object\nids. With --squashed, a file that upstream also changed counts when merging\nthe branch's version into upstream's leaves it unchanged.\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-irrelevant <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  22 +-\n builtin/branch.c                 | 355 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 217 +++++++++++++++++++\n 4 files changed, 572 insertions(+), 30 deletions(-)\n\n\nbase-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v4\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v3:\n\n 1:  82413e5cd7 ! 1:  1d01085b75 branch: move the deleteMerged opt-out check into a helper\n     @@ Metadata\n       ## Commit message ##\n          branch: move the deleteMerged opt-out check into a helper\n      \n     -    The next commit lets \"git branch --delete-merged\" delete branches whose\n     -    tips are not reachable from their upstream, and those must honor\n     -    branch.<name>.deleteMerged=false as well. Move the check into a helper\n     -    so it can be used in both places.\n     +    A later commit adds \"git branch --delete-integrated\", which deletes\n     +    branches whose tips are not reachable from their upstream. Those must\n     +    honor branch.<name>.deleteMerged=false as well. Move the check into a\n     +    helper so it can be used in both places.\n      \n          No change in behavior.\n      \n -:  ---------- > 2:  592562d7e7 branch: let delete_branches() name where a branch landed\n 2:  9a7e88ced6 ! 3:  deb1e649cf branch: let --delete-merged find rebase merged branches\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    branch: let --delete-merged find rebase merged branches\n     +    branch: add --delete-integrated to delete rebase and squash merged branches\n      \n          A branch that upstream integrated with\n      \n     @@ Commit message\n              git checkout main\n              git merge --ff-only topic\n      \n     +    or with\n     +\n     +        git merge --squash topic\n     +        git commit\n     +\n          is never deleted by \"git branch --delete-merged\". This is what the\n     -    \"Rebase and merge\" button on GitHub does. The upstream holds rewritten\n     -    copies of the branch's commits, so its tip is not reachable from it and\n     -    it looks unmerged forever.\n     +    \"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\n     +    upstream holds rewritten copies of the branch's work, so its tip is not\n     +    reachable from it and it looks unmerged forever.\n      \n     -    Treat such a branch as merged when some upstream commit since the fork\n     -    point has the branch's version of every file the branch changed. Name\n     -    that commit in the output so the user can see where the work went:\n     +    Add --delete-integrated, which does what --delete-merged does and also\n     +    deletes a branch when some upstream commit since the fork point has the\n     +    branch's version of every file the branch changed. That commit is named\n     +    in the output so the user can see where the work went:\n      \n              Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n      \n     -    The first upstream commit that has them is used, so the branch is\n     -    deleted even if upstream later reverted or reworked them. Nothing is\n     -    lost, since that commit keeps them in the upstream history. A branch\n     -    whose changes only partly landed is kept.\n     +    The first such commit is used, so the branch is deleted even if\n     +    upstream later reverted or reworked the changes. Nothing is lost, since\n     +    that commit keeps them in the upstream history. Who made that commit\n     +    does not matter, since the local branch is no longer needed either way.\n     +    A branch whose changes only partly landed is kept.\n      \n     -    The same check finds a squash merge when upstream did not change the\n     -    same files between the fork point and the squash. It never reads file\n     -    contents, so it stays cheap in a partial clone.\n     +    --delete-merged keeps deleting only branches whose own commits are in\n     +    the upstream, so it never removes a branch because somebody else made\n     +    the same change.\n     +\n     +    The check never reads file contents, so it stays cheap in a partial\n     +    clone.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     + ## Documentation/config/branch.adoc ##\n     +@@ Documentation/config/branch.adoc: for details).\n     + \n     + `branch.<name>.deleteMerged`::\n     + \tIf set to `false`, branch _<name>_ is exempt from\n     +-\t`git branch --delete-merged`.  Useful for a topic branch you\n     +-\tintend to develop further after an initial round has been\n     +-\tmerged upstream.  Defaults to true.  Explicit deletion via\n     +-\t`git branch -d` is unaffected.\n     ++\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n     ++\tfor a topic branch you intend to develop further after an\n     ++\tinitial round has been merged upstream.  Defaults to true.\n     ++\tExplicit deletion via `git branch -d` is unaffected.\n     +\n       ## Documentation/git-branch.adoc ##\n     -@@ Documentation/git-branch.adoc: This option is only applicable in non-verbose mode.\n     +@@ Documentation/git-branch.adoc: git branch (-c|-C) [<old-branch>] <new-branch>\n     + git branch (-d|-D) [-r] <branch-name>...\n     + git branch --edit-description [<branch-name>]\n     + git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n     ++git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n     + \n     + DESCRIPTION\n     + -----------\n     +@@ Documentation/git-branch.adoc: A branch whose work has not yet been merged into its upstream is\n     + silently skipped. Delete it with `git branch -D` if you want to\n     + remove it anyway.\n       \n     - `--delete-merged <pattern>`::\n     - \tDelete local branches whose configured upstream matches\n     --\t_<pattern>_, but only when their tip is reachable from that\n     --\tupstream. In other words, the work on the branch has already\n     --\tlanded on the upstream it tracks, so the local copy is no longer\n     --\tneeded. _<pattern>_ may name a ref, a remote (using the branch its\n     --\t`HEAD` points at), or a shell-style glob. The option can be\n     --\trepeated to widen the upstream match.\n     -+\t_<pattern>_, but only when their work has already landed on that\n     -+\tupstream, so the local copy is no longer needed. This is the case\n     -+\twhen their tip is reachable from the upstream, or when some\n     -+\tupstream commit has their version of every file they changed, as\n     -+\thappens after a rebase merge, even if those changes were later\n     -+\treverted. The message for such a branch names the first upstream\n     -+\tcommit that has them. _<pattern>_ may name a ref, a remote (using\n     -+\tthe branch its `HEAD` points at), or a shell-style glob. The\n     -+\toption can be repeated to widen the upstream match.\n     - \tOptional _<branch-pattern>_ arguments limit which local branches\n     - \tare considered, e.g. `git branch --delete-merged 'origin/*'\n     - \t'topic-*'`.\n     ++`--delete-integrated <pattern>`::\n     ++\tLike `--delete-merged`, but also delete a branch whose changes\n     ++\tthe upstream already has even though its commits are not there,\n     ++\tas happens after a squash or rebase merge. Such a branch is\n     ++\tdeleted when some upstream commit since the fork point has its\n     ++\tversion of every file it changed, even if those changes were\n     ++\tlater reverted, and the message names the first such commit.\n     ++\tIt does not matter who made that commit. The same rules for\n     ++\twhich branches are kept apply as for `--delete-merged`.\n     ++\n     + `--dry-run`::\n     +-\tWith `--delete-merged`, print which branches would be\n     +-\tdeleted and exit without touching any ref.  Useful for\n     +-\tsanity-checking a wide pattern like `'origin/*'` before\n     +-\tcommitting to the deletion.\n     ++\tWith `--delete-merged` or `--delete-integrated`, print which\n     ++\tbranches would be deleted and exit without touching any ref.\n     ++\tUseful for sanity-checking a wide pattern like `'origin/*'`\n     ++\tbefore committing to the deletion.\n     + \n     + `-v`::\n     + `-vv`::\n      \n       ## builtin/branch.c ##\n      @@\n     @@ builtin/branch.c\n       \n       static const char * const builtin_branch_usage[] = {\n       \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n     -@@ builtin/branch.c: static void delete_branch_config(const char *branchname)\n     - }\n     +@@ builtin/branch.c: enum delete_branch_flags {\n     + \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n     + \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n     + \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n     ++\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n     + };\n       \n     - static int delete_branches(int argc, const char **argv, int kinds,\n     --\t\t\t   unsigned int flags)\n     -+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n     - {\n     - \tstruct commit *head_rev = NULL;\n     - \tstruct object_id oid;\n     -@@ builtin/branch.c: static int delete_branches(int argc, const char **argv, int kinds,\n     - \t\t}\n     - \n     - \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n     -+\t\t    !(landed_commits &&\n     -+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n     - \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n     - \t\t\t\t\tflags)) {\n     - \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n     -@@ builtin/branch.c: static int delete_branches(int argc, const char **argv, int kinds,\n     - \tfor_each_string_list_item(item, &refs_to_delete) {\n     - \t\tchar *describe_ref = item->util;\n     - \t\tchar *name = item->string;\n     -+\t\tstruct commit *landed = landed_commits ?\n     -+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n     -+\t\tconst char *landed_abbrev = landed ?\n     -+\t\t\trepo_find_unique_abbrev(the_repository,\n     -+\t\t\t\t\t\t&landed->object.oid,\n     -+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n     -+\n     - \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n     --\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n     -+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n     -+\t\t\t\t;\n     -+\t\t\telse if (landed)\n     -+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n     -+\t\t\t\t       name + branch_name_pos, describe_ref,\n     -+\t\t\t\t       landed_abbrev);\n     -+\t\t\telse\n     - \t\t\t\tprintf(remote_branch\n     - \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n     - \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n     - \t\t\t\t\tname + branch_name_pos, describe_ref);\n     - \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n     - \t\t\tchar *refname = name + branch_name_pos;\n     --\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n     -+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n     -+\t\t\t\t;\n     -+\t\t\telse if (landed)\n     -+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n     -+\t\t\t\t       refname, describe_ref, landed_abbrev);\n     -+\t\t\telse\n     - \t\t\t\tprintf(remote_branch\n     - \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n     - \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n     + static int check_branch_commit(const char *branchname, const char *refname,\n      @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n       \treturn ret;\n       }\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n      -\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n      +\t\t\t\t\tFILTER_REFS_BRANCHES,\n      +\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n     -+\t\t\tstruct commit *rev = lookup_commit_reference(\n     -+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n     -+\t\t\tstruct commit *upstream = lookup_commit_reference_by_name(\n     -+\t\t\t\tupstream_refname);\n     ++\t\t\tstruct commit *rev, *upstream;\n      +\n     ++\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n     ++\t\t\t\tcontinue;\n     ++\t\t\trev = lookup_commit_reference(\n     ++\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n     ++\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n      +\t\t\tif (!rev || !upstream)\n      +\t\t\t\tcontinue;\n      +\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \t\t\t\t      FILTER_REFS_BRANCHES,\n       \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n       \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n     --\t\t\t\t      flags);\n     +-\t\t\t\t      flags, NULL);\n      +\t\t\t\t      flags, &landed_commits);\n       \n       \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n     @@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstrea\n       \tstrset_clear(&deletable_branch_names);\n       \tref_array_clear(&candidates);\n      @@ builtin/branch.c: int cmd_branch(int argc,\n     - \t\t\tdie(_(\"branch name required\"));\n     - \t\tret = delete_branches(argc, argv, filter.kind,\n     - \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n     --\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n     -+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n     + \tint delete = 0, rename = 0, copy = 0, list = 0,\n     + \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n     + \tstruct strvec delete_merged = STRVEC_INIT;\n     ++\tstruct strvec delete_integrated = STRVEC_INIT;\n     + \tint dry_run = 0;\n     + \tconst char *new_upstream = NULL;\n     + \tint noncreate_actions = 0;\n     +@@ builtin/branch.c: int cmd_branch(int argc,\n     + \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n     + \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n     + \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n     ++\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n     ++\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n     ++\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n     + \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n     +-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n     ++\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n     + \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n     + \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n     + \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n     +@@ builtin/branch.c: int cmd_branch(int argc,\n     + \n     + \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n     + \t    !show_current && !unset_upstream && !delete_merged.nr &&\n     +-\t    argc == 0)\n     ++\t    !delete_integrated.nr && argc == 0)\n     + \t\tlist = 1;\n     + \n     + \tif (filter.with_commit || filter.no_commit ||\n     +@@ builtin/branch.c: int cmd_branch(int argc,\n     + \n     + \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n     + \t\t\t    !!show_current + !!list + !!edit_description +\n     +-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n     ++\t\t\t    !!unset_upstream + !!delete_merged.nr +\n     ++\t\t\t    !!delete_integrated.nr;\n     + \tif (noncreate_actions > 1)\n     + \t\tusage_with_options(builtin_branch_usage, options);\n     + \n     +-\tif (dry_run && !delete_merged.nr)\n     +-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n     ++\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n     ++\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n     + \n     + \tif (recurse_submodules_explicit) {\n     + \t\tif (!submodule_propagate_branches)\n     +@@ builtin/branch.c: int cmd_branch(int argc,\n     + \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n     + \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n       \t\tgoto out;\n     - \t} else if (delete_merged.nr) {\n     - \t\tret = delete_merged_branches(&delete_merged, argv,\n     ++\t} else if (delete_integrated.nr) {\n     ++\t\tret = delete_merged_branches(&delete_integrated, argv,\n     ++\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n     ++\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n     ++\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n     ++\t\tgoto out;\n     + \t} else if (show_current) {\n     + \t\tprint_current_branch_name();\n     + \t\tret = 0;\n     +@@ builtin/branch.c: int cmd_branch(int argc,\n     + \n     + out:\n     + \tstrvec_clear(&delete_merged);\n     ++\tstrvec_clear(&delete_integrated);\n     + \tstring_list_clear(&sorting_options, 0);\n     + \treturn ret;\n     + }\n      \n       ## t/t3200-branch.sh ##\n     +@@ t/t3200-branch.sh: check_branches () {\n     + \ttest_cmp expect actual\n     + }\n     + \n     ++test_expect_success '--delete-merged does not delete unrelated branches' '\n     ++\tsetup_repo_for_delete_merged &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit checkout -b unrelated-topic --track origin/next &&\n     ++\t\ttest_write_lines 1 2 3 4 >file &&\n     ++\t\tgit add file &&\n     ++\t\tgit commit -m \"add file\" &&\n     ++\t\tgit push origin unrelated-topic &&\n     ++\t\tgit checkout --detach\n     ++\t) &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 >file &&\n     ++\t\ttest_write_lines 5 6 7 >elif &&\n     ++\t\tgit add file elif &&\n     ++\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n     ++\t\t\t-m \"unrelated commit adding an identical file\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\ttest_must_be_empty actual &&\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\tmain\n     ++\t\tunrelated-topic\n     ++\t\tEOF\n     ++\t)\n     ++'\n     ++\n     + test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n     + \tsetup_repo_for_delete_merged &&\n     + \t(\n      @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected merged branches' '\n       \t)\n       '\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +}\n      +\n     -+test_expect_success '--delete-merged deletes a rebase merged branch' '\n     ++test_expect_success '--delete-integrated deletes a rebase merged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic rebased rebased-one rebased-two &&\n      +\t(\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t\tgit fetch origin &&\n      +\t\tsha=$(git rev-parse --short rebased) &&\n      +\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n      +\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +'\n      +\n     -+test_expect_success '--delete-merged deletes a squash merged branch' '\n     ++test_expect_success '--delete-integrated deletes a squash merged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic squashed squashed-one squashed-two &&\n      +\tpush_topic partial lands-upstream stays-local &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t\tgit fetch origin &&\n      +\t\tsha=$(git rev-parse --short squashed) &&\n      +\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n      +\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +'\n      +\n     -+test_expect_success '--delete-merged deletes a squash merged branch that was reverted' '\n     ++test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic reverted reverted-work &&\n      +\tsquash_merge_upstream reverted &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t\tgit fetch origin &&\n      +\t\tsha=$(git rev-parse --short reverted) &&\n      +\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n      +\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t)\n      +'\n      +\n     -+test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n     ++test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n      +\tsetup_repo_for_delete_merged &&\n      +\tpush_topic a-unmerged unmerged-work &&\n      +\tpush_topic b-merged merged-work &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n      +\t\tgit fetch origin &&\n      +\t\tsha=$(git rev-parse --short b-merged) &&\n      +\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n      +\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes only selected me\n       test_expect_success '--delete-merged keeps main despite a different default push remote' '\n       \tsetup_repo_for_delete_merged &&\n       \tcreate_merged_branch on-next &&\n     +@@ t/t3200-branch.sh: test_expect_success '--dry-run without --delete-merged is rejected' '\n     + \ttest_grep \"requires --delete-merged\" err\n     + '\n     + \n     ++test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n     ++\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n     ++\t\t--delete-integrated origin/next\n     ++'\n     ++\n     + test_done\n 3:  2befb182dc ! 4:  19a9630c13 branch: add --squashed to find squash merged branches\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    branch: add --squashed to find squash merged branches\n     +    branch: let --delete-integrated find branches merged onto changed files\n      \n          A branch that upstream integrated with\n      \n     @@ Commit message\n              git commit\n      \n          after upstream had also changed the files the branch touches is still\n     -    kept by \"git branch --delete-merged\". The squash commit has neither the\n     -    branch's version of those files nor the fork point's, so the previous\n     -    check cannot see that the work landed. This happens all the time on\n     -    hosting services, where the \"Squash and merge\" button on GitHub lands\n     -    pull requests this way while the main branch keeps moving. A rebase\n     -    merge onto such changes looks the same.\n     +    kept by \"git branch --delete-integrated\". The squash commit has neither\n     +    the branch's version of those files nor the fork point's, so the\n     +    previous check cannot see that the work landed. This happens all the\n     +    time on hosting services, where the \"Squash and merge\" button on GitHub\n     +    lands pull requests this way while the main branch keeps moving. A\n     +    rebase merge onto such changes looks the same.\n      \n     -    Add --squashed, which for each such file merges the branch's version\n     -    into the upstream one and treats the change as landed when that leaves\n     -    the upstream version unchanged. This has to read the files, so it is\n     -    slower, and in a partial clone it may download blobs. It is therefore\n     -    only done when asked for.\n     +    For each such file, merge the branch's version into the upstream one\n     +    and treat the change as landed when that leaves the upstream version\n     +    unchanged. This has to read the files, so in a partial clone it may\n     +    download blobs.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/git-branch.adoc ##\n     -@@ Documentation/git-branch.adoc: git branch (-m|-M) [<old-branch>] <new-branch>\n     - git branch (-c|-C) [<old-branch>] <new-branch>\n     - git branch (-d|-D) [-r] <branch-name>...\n     - git branch --edit-description [<branch-name>]\n     --git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n     -+git branch [--dry-run] [--squashed] (--delete-merged <pattern>)... [<branch-pattern>...]\n     - \n     - DESCRIPTION\n     - -----------\n      @@ Documentation/git-branch.adoc: remove it anyway.\n     - \tsanity-checking a wide pattern like `'origin/*'` before\n     - \tcommitting to the deletion.\n     - \n     -+`--squashed`::\n     -+\tWith `--delete-merged`, also delete a branch that was squash or\n     -+\trebase merged after upstream changed the same files. Upstream\n     -+\tthen has neither the branch's version of those files nor the fork\n     -+\tpoint's, so the branch's version is merged into upstream's, and\n     -+\tthe file counts as landed when that leaves upstream's version\n     -+\tunchanged. This reads those files, so it takes longer and may\n     -+\tdownload blobs in a partial clone.\n     -+\n     - `-v`::\n     - `-vv`::\n     - `--verbose`::\n     + \tLike `--delete-merged`, but also delete a branch whose changes\n     + \tthe upstream already has even though its commits are not there,\n     + \tas happens after a squash or rebase merge. Such a branch is\n     +-\tdeleted when some upstream commit since the fork point has its\n     +-\tversion of every file it changed, even if those changes were\n     +-\tlater reverted, and the message names the first such commit.\n     +-\tIt does not matter who made that commit. The same rules for\n     +-\twhich branches are kept apply as for `--delete-merged`.\n     ++\tdeleted when some upstream commit since the fork point has all\n     ++\tof its changes, even if they were later reverted, and the\n     ++\tmessage names the first such commit. It does not matter who made\n     ++\tthat commit. A file counts when the commit has the branch's\n     ++\tversion of it, or when merging the branch's version into the\n     ++\tcommit's leaves it unchanged, which reads the file and may\n     ++\tdownload blobs in a partial clone. The same rules for which\n     ++\tbranches are kept apply as for `--delete-merged`.\n     + \n     + `--dry-run`::\n     + \tWith `--delete-merged` or `--delete-integrated`, print which\n      \n       ## builtin/branch.c ##\n      @@\n     @@ builtin/branch.c\n       \n       static const char * const builtin_branch_usage[] = {\n       \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n     -@@ builtin/branch.c: enum delete_branch_flags {\n     - \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n     - \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n     - \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n     -+\tDELETE_BRANCH_SQUASHED = (1 << 5),\n     - };\n     - \n     - static int check_branch_commit(const char *branchname, const char *refname,\n      @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n       \treturn ret;\n       }\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n       }\n       \n       static int changes_landed(struct landing_search *search,\n     --\t\t\t  struct commit *commit)\n     -+\t\t\t  struct commit *commit, unsigned int flags)\n     + \t\t\t  struct commit *commit)\n       {\n      -\tfor (size_t i = 0; i < search->changes_nr; i++)\n      -\t\tif (!change_landed(&search->changes[i], commit))\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n      +\t\tstruct branch_change *change = &search->changes[i];\n      +\n      +\t\tchange->state = change_state_at(change, commit);\n     -+\t\tif (change->state == CHANGE_MISSING ||\n     -+\t\t    (change->state == CHANGE_NEEDS_MERGE &&\n     -+\t\t     !(flags & DELETE_BRANCH_SQUASHED)))\n     ++\t\tif (change->state == CHANGE_MISSING)\n       \t\t\treturn 0;\n      +\t}\n      +\tfor (size_t i = 0; i < search->changes_nr; i++) {\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n       \treturn 1;\n       }\n       \n     -@@ builtin/branch.c: static int touches_changes(struct string_list *paths,\n     - \n     - static struct commit *find_landed_commit(struct landing_search *search,\n     - \t\t\t\t\t struct changed_paths *cache,\n     --\t\t\t\t\t struct diff_options *opt)\n     -+\t\t\t\t\t struct diff_options *opt,\n     -+\t\t\t\t\t unsigned int flags)\n     - {\n     - \tstruct commit *commit, *landed = NULL;\n     - \tstruct strvec args = STRVEC_INIT;\n     -@@ builtin/branch.c: static struct commit *find_landed_commit(struct landing_search *search,\n     - \twhile (!landed && (commit = get_revision(&revs)))\n     - \t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n     - \t\t\t\t    search) &&\n     --\t\t    changes_landed(search, commit))\n     -+\t\t    changes_landed(search, commit, flags))\n     - \t\t\tlanded = commit;\n     - \trelease_revisions(&revs);\n     - \tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n     -@@ builtin/branch.c: static struct commit *find_landed_commit(struct landing_search *search,\n     - \n     - static void find_landed_commits(struct landing_search *searches,\n     - \t\t\t\tsize_t searches_nr,\n     --\t\t\t\tstruct strmap *landed_commits)\n     -+\t\t\t\tstruct strmap *landed_commits,\n     -+\t\t\t\tunsigned int flags)\n     - {\n     - \tstruct changed_paths cache;\n     - \tstruct diff_options opt;\n     -@@ builtin/branch.c: static void find_landed_commits(struct landing_search *searches,\n     - \n     - \t\tif (!searches[i].changes_nr)\n     - \t\t\tcontinue;\n     --\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n     -+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt, flags);\n     - \t\tif (landed)\n     - \t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n     - \t\t\t\t   landed);\n     -@@ builtin/branch.c: static int delete_merged_branches(const struct strvec *upstreams,\n     - \t\t\tstrset_add(&deletable_branch_names, branch_name);\n     - \t}\n     - \n     --\tfind_landed_commits(searches, searches_nr, &landed_commits);\n     -+\tfind_landed_commits(searches, searches_nr, &landed_commits, flags);\n     - \tfor (size_t i = 0; i < searches_nr; i++) {\n     - \t\tconst char *branch_name = searches[i].branch_name;\n     - \n     -@@ builtin/branch.c: int cmd_branch(int argc,\n     - \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n     - \tstruct strvec delete_merged = STRVEC_INIT;\n     - \tint dry_run = 0;\n     -+\tint squashed = 0;\n     - \tconst char *new_upstream = NULL;\n     - \tint noncreate_actions = 0;\n     - \t/* possible options */\n     -@@ builtin/branch.c: int cmd_branch(int argc,\n     - \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n     - \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n     - \t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n     -+\t\tOPT_BOOL(0, \"squashed\", &squashed,\n     -+\t\t\tN_(\"with --delete-merged, also find branches merged after upstream changed their files\")),\n     - \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n     - \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n     - \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n     -@@ builtin/branch.c: int cmd_branch(int argc,\n     - \n     - \tif (dry_run && !delete_merged.nr)\n     - \t\tdie(_(\"--dry-run requires --delete-merged\"));\n     -+\tif (squashed && !delete_merged.nr)\n     -+\t\tdie(_(\"--squashed requires --delete-merged\"));\n     - \n     - \tif (recurse_submodules_explicit) {\n     - \t\tif (!submodule_propagate_branches)\n     -@@ builtin/branch.c: int cmd_branch(int argc,\n     - \t} else if (delete_merged.nr) {\n     - \t\tret = delete_merged_branches(&delete_merged, argv,\n     - \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n     --\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n     -+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n     -+\t\t\t\t\t     (squashed ? DELETE_BRANCH_SQUASHED : 0));\n     - \t\tgoto out;\n     - \t} else if (show_current) {\n     - \t\tprint_current_branch_name();\n      \n       ## t/t3200-branch.sh ##\n     -@@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes a squash merged branch that was rev\n     +@@ t/t3200-branch.sh: test_expect_success '--delete-integrated deletes a squash merged branch that was\n       \t)\n       '\n       \n     -+test_expect_success '--delete-merged --squashed deletes a squash merged branch after upstream changed the same file' '\n     ++test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n      +\t(\n      +\t\tcd upstream &&\n      +\t\tgit checkout next &&\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes a squash merged\n      +\t\tgit fetch origin &&\n      +\t\tsha=$(git rev-parse --short edits-shared) &&\n      +\n     -+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n     -+\t\ttest_must_be_empty actual &&\n     -+\t\tcheck_branches <<-\\EOF &&\n     -+\t\tedits-shared\n     -+\t\tmain\n     -+\t\tEOF\n     -+\n     -+\t\tgit branch --delete-merged origin/next --squashed \\\n     -+\t\t\t>actual 2>&1 &&\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n      +\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n      +\t\ttest_cmp expect actual &&\n      +\n     @@ t/t3200-branch.sh: test_expect_success '--delete-merged deletes a squash merged\n      +\t)\n      +'\n      +\n     - test_expect_success '--delete-merged reports a merge plainly next to an unmerged branch' '\n     + test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n       \tsetup_repo_for_delete_merged &&\n       \tpush_topic a-unmerged unmerged-work &&\n     -@@ t/t3200-branch.sh: test_expect_success \"branch -d still deletes a deleteMerged=false branch\" '\n     - \t)\n     - '\n     - \n     -+test_expect_success '--squashed without --delete-merged is rejected' '\n     -+\ttest_must_fail git -C forked branch --squashed 2>err &&\n     -+\ttest_grep \"requires --delete-merged\" err\n     -+'\n     -+\n     - test_expect_success '--dry-run without --delete-merged is rejected' '\n     - \ttest_must_fail git -C forked branch --dry-run 2>err &&\n     - \ttest_grep \"requires --delete-merged\" err\n\n-- \ngitgitgadget\n\n"},{"id":"556276","messageId":"1d01085b7526ef90ab762c010550e99494e94e3d.1793003627.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","subject":"[PATCH v4 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-26T08:33:44Z","receivedAt":"2026-10-26T08:33:44Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..171b13b081 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"556277","messageId":"592562d7e766a0fc40a5ed3c5924e64582fc5505.1793003627.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","subject":"[PATCH v4 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-26T08:33:45Z","receivedAt":"2026-10-26T08:33:45Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 171b13b081..99b0dd45e9 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"556278","messageId":"deb1e649cfe0c3d83af5d7fca01e8f5b6bf66359.1793003627.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","subject":"[PATCH v4 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-26T08:33:46Z","receivedAt":"2026-10-26T08:33:46Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 248 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 175 ++++++++++++++++++++++\n 4 files changed, 435 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..6be28c8a4e 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 99b0dd45e9..ba04d8c486 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -201,6 +207,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +851,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options opt;\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&opt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&opt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *opt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(opt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *opt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options opt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\topt.no_free = 1;\n+\tdiff_setup_done(&opt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\topt.no_free = 0;\n+\tdiff_free(&opt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1055,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1092,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1136,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1144,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1223,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1281,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1334,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1118,12 +1344,13 @@ int cmd_branch(int argc,\n \n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1395,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1340,6 +1573,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..64dc4c7561 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1925,6 +1925,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -1979,6 +2012,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2178,4 +2348,9 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"556279","messageId":"19a9630c138500091fdec104dbd77f4e1fc48cd7.1793003627.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","subject":"[PATCH v4 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-26T08:33:47Z","receivedAt":"2026-10-26T08:33:47Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services, where the \"Squash and merge\" button on GitHub\nlands pull requests this way while the main branch keeps moving. A\nrebase merge onto such changes looks the same.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 13 ++++---\n builtin/branch.c              | 71 ++++++++++++++++++++++++++++++-----\n t/t3200-branch.sh             | 42 +++++++++++++++++++++\n 3 files changed, 111 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 6be28c8a4e..489a9d5fd0 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,14 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it, or when merging the branch's version into the\n+\tcommit's leaves it unchanged, which reads the file and may\n+\tdownload blobs in a partial clone. The same rules for which\n+\tbranches are kept apply as for `--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex ba04d8c486..c2343f3164 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -851,10 +853,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -886,6 +895,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -893,25 +904,65 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&opt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 64dc4c7561..ecded6085b 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2122,6 +2122,48 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"556726","messageId":"20261029043041.5994-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"pull.2425.v4.git.git.1793003627.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 0/4] branch: let --delete-merged find squash merged branches","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-10-29T04:30:41Z","receivedAt":"2026-10-29T04:30:41Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Changes in v4:\n>\n>  * --delete-merged works as it does on master again: it only deletes\n>    branches whose tip is reachable from their upstream\n\nThanks. I ran v4 (19a9630c13) on the same generated repositories as\nbefore, against master (7e34d8174d). t3200 passes.\n\n--delete-merged now gives exactly the same result as master on every\ncase, including the branch that was never merged but whose change\nanother author made upstream. That one is kept.\n\n--delete-integrated deletes the squash merges, the GitHub-style\nsquash, the rebase merge, the rename, the squash that was later\nreverted, and a squash that landed after upstream changed another line\nof the same file. \"landed as\" names the correct commit each time. It\nkeeps a branch whose work only partly landed, a cherry-pick whose\nconflict was resolved differently, and the never-merged branches with\nan empty or net-zero diff. As the documentation says, it deletes the\nbranch whose change another author made, which is fine now that the\nuser has to ask for it.\n\nThree small things:\n\n * \"git branch -h\" does not show --delete-integrated in the usage\n   lines, although the SYNOPSIS in git-branch.adoc has it.\n\n * Giving both --delete-merged and --delete-integrated dies with the\n   full usage text. A short \"cannot be used together\" message would be\n   clearer.\n\n * The cover letter still says \"--squashed\" in the second paragraph,\n   and \"--delete-irrelevant\" in the list of changes.\n\nA branch checked out in a worktree is still skipped with no message,\nwith both options.\n\nTested-by: Jasper Mayone <me@jaspermayone.com>\n\nThanks,\nJasper\n\n"},{"id":"556739","messageId":"CAHwyqnV2XrqLmvfKXXqrk3WdHsan_NeoT2rLH-ToxvhnNxumZA@mail.gmail.com","threadId":"66417","inReplyTo":"20261029043041.5994-1-me@jaspermayone.com","subject":"Re: [PATCH v4 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-29T08:30:25Z","receivedAt":"2026-10-29T08:30:25Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":">  * \"git branch -h\" does not show --delete-integrated in the usage\n>    lines, although the SYNOPSIS in git-branch.adoc has it.\n\nGood point.\n\n>  * Giving both --delete-merged and --delete-integrated dies with the\n>    full usage text. A short \"cannot be used together\" message would be\n>    clearer.\n\nThanks, I will fix this.\n\n\nHarald\n\n"},{"id":"556902","messageId":"pull.2425.v5.git.git.1793357582.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v5 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:58Z","receivedAt":"2026-10-30T10:52:58Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v5:\n\n * git branch -h lists --delete-integrated in the usage lines\n * Giving both --delete-merged and --delete-integrated dies with a short\n   \"cannot be used together\" message instead of the full usage text\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-integrated <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  22 +-\n builtin/branch.c                 | 360 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 218 +++++++++++++++++++\n 4 files changed, 578 insertions(+), 30 deletions(-)\n\n\nbase-commit: 8d1b215c01512b9658d4525c54ff9b9245f9a7e8\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v5\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v4:\n\n 1:  1d01085b75 = 1:  090aec5725 branch: move the deleteMerged opt-out check into a helper\n 2:  592562d7e7 = 2:  24180bf4a2 branch: let delete_branches() name where a branch landed\n 3:  deb1e649cf ! 3:  bf30f57ceb branch: add --delete-integrated to delete rebase and squash merged branches\n     @@ builtin/branch.c\n       \n       static const char * const builtin_branch_usage[] = {\n       \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n     +@@ builtin/branch.c: static const char * const builtin_branch_usage[] = {\n     + \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n     + \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n     + \t   \"[<branch-pattern>...]\"),\n     ++\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n     ++\t   \"[<branch-pattern>...]\"),\n     + \tNULL\n     + };\n     + \n      @@ builtin/branch.c: enum delete_branch_flags {\n       \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n       \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n     @@ builtin/branch.c: int cmd_branch(int argc,\n       \n       \tif (filter.with_commit || filter.no_commit ||\n      @@ builtin/branch.c: int cmd_branch(int argc,\n     + \t    filter.points_at.nr || filter.forked.nr)\n     + \t\tlist = 1;\n       \n     ++\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n     ++\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n     ++\n       \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n       \t\t\t    !!show_current + !!list + !!edit_description +\n      -\t\t\t    !!unset_upstream + !!delete_merged.nr;\n     @@ t/t3200-branch.sh: test_expect_success '--dry-run without --delete-merged is rej\n       \n      +test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n      +\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n     -+\t\t--delete-integrated origin/next\n     ++\t\t--delete-integrated origin/next 2>err &&\n     ++\ttest_grep \"cannot be used together\" err\n      +'\n      +\n       test_done\n 4:  19a9630c13 = 4:  9443055242 branch: let --delete-integrated find branches merged onto changed files\n\n-- \ngitgitgadget\n\n"},{"id":"556903","messageId":"090aec572530181b11ff4232f36e20908d0041a7.1793357582.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v5.git.git.1793357582.gitgitgadget@gmail.com","subject":"[PATCH v5 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:59Z","receivedAt":"2026-10-30T10:52:59Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..171b13b081 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"556904","messageId":"24180bf4a2163188632efe6c234888e3247fb532.1793357582.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v5.git.git.1793357582.gitgitgadget@gmail.com","subject":"[PATCH v5 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:53:00Z","receivedAt":"2026-10-30T10:53:00Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 171b13b081..99b0dd45e9 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"556905","messageId":"bf30f57ceb77c5052a72aa7dc01e3bb163993a63.1793357582.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v5.git.git.1793357582.gitgitgadget@gmail.com","subject":"[PATCH v5 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:53:01Z","receivedAt":"2026-10-30T10:53:01Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 253 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 176 +++++++++++++++++++++\n 4 files changed, 441 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..6be28c8a4e 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 99b0dd45e9..ac2dd68b23 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -41,6 +47,8 @@ static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n \t   \"[<branch-pattern>...]\"),\n+\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n+\t   \"[<branch-pattern>...]\"),\n \tNULL\n };\n \n@@ -201,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +853,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options opt;\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&opt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&opt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *opt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", opt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(opt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *opt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options opt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &opt);\n+\topt.flags.recursive = 1;\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\topt.no_free = 1;\n+\tdiff_setup_done(&opt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\topt.no_free = 0;\n+\tdiff_free(&opt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1057,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1094,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1138,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1146,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1225,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1283,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1336,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1116,14 +1344,18 @@ int cmd_branch(int argc,\n \t    filter.points_at.nr || filter.forked.nr)\n \t\tlist = 1;\n \n+\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n+\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n+\n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1400,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1340,6 +1578,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..1fbb173ad4 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1925,6 +1925,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -1979,6 +2012,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2178,4 +2348,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"556906","messageId":"9443055242b8968e7196530bc67614d5b43d1a45.1793357582.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v5.git.git.1793357582.gitgitgadget@gmail.com","subject":"[PATCH v5 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:53:02Z","receivedAt":"2026-10-30T10:53:02Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services, where the \"Squash and merge\" button on GitHub\nlands pull requests this way while the main branch keeps moving. A\nrebase merge onto such changes looks the same.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 13 ++++---\n builtin/branch.c              | 71 ++++++++++++++++++++++++++++++-----\n t/t3200-branch.sh             | 42 +++++++++++++++++++++\n 3 files changed, 111 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 6be28c8a4e..489a9d5fd0 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,14 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it, or when merging the branch's version into the\n+\tcommit's leaves it unchanged, which reads the file and may\n+\tdownload blobs in a partial clone. The same rules for which\n+\tbranches are kept apply as for `--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex ac2dd68b23..36e67830e7 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -853,10 +855,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -888,6 +897,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -895,25 +906,65 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&opt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 1fbb173ad4..d4ebbd0ae9 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2122,6 +2122,48 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"556953","messageId":"xmqq33tnvw58.fsf@gitster.g","threadId":"66417","inReplyTo":"bf30f57ceb77c5052a72aa7dc01e3bb163993a63.1793357582.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v5 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-30T19:14:11Z","receivedAt":"2026-10-30T19:14:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> +static void find_landed_commits(struct landing_search *searches,\n> +\t\t\t\tsize_t searches_nr,\n> +\t\t\t\tstruct strmap *landed_commits)\n> +{\n> +\tstruct changed_paths cache;\n> +\tstruct diff_options opt;\n\nA tiny nit, but because so many things are called x_option in our\ncodebase, I am used to seeing the variable also carry \"x\" to match,\nor the use of mere \"opt\" down below would make us go back to see\nwhat kind of option it refers to while reading the code.  I'd liked\nto see this called \"diffopt\" instead.\n\n> +\trepo_diff_setup(the_repository, &opt);\n> +\topt.flags.recursive = 1;\n> +\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n> +\topt.no_free = 1;\n> +\tdiff_setup_done(&opt);\n> +\tinit_changed_paths(&cache);\n\nThe .no_free member is set to avoid the diff_free() call made in\nchanged_path_of(), which in turn is called by find_landed_commit().\nEverytime I see manipulation of .no_free member, it makes me look at\nthe code twice but this seems to be a good use of the feature.  And ...\n\n> +\tfor (size_t i = 0; i < searches_nr; i++) {\n> +\t\tstruct commit *landed;\n> +\n> +\t\tif (!searches[i].changes_nr)\n> +\t\t\tcontinue;\n> +\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n> +\t\tif (landed)\n> +\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n> +\t\t\t\t   landed);\n> +\t}\n> +\n> +\tdeep_clear_changed_paths(&cache, free_changed_paths);\n> +\topt.no_free = 0;\n> +\tdiff_free(&opt);\n\n... clearing the member before calling diff_free() to release the\nresources is also in line with the established practice.\n\nI didn't have time to look at other parts of this patch (or other\npatches in the series), but the use of .no_free in this function\nlooked OK.\n\nThanks.\n\n"},{"id":"556954","messageId":"xmqqwlqzuh1p.fsf@gitster.g","threadId":"66417","inReplyTo":"9443055242b8968e7196530bc67614d5b43d1a45.1793357582.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v5 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-30T19:25:38Z","receivedAt":"2026-10-30T19:25:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> +static int merge_keeps_upstream(const struct branch_change *change)\n> +{\n> +\tmmfile_t base, upstream, branch;\n> +\tmmfile_t result = { 0 };\n> +\tint ret;\n> +\n> +\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n> +\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n> +\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n> +\tret = ll_merge(&result, change->path, &base, \"base\",\n> +\t\t       &upstream, \"upstream\", &branch, \"branch\",\n> +\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n> +\t      result.size == upstream.size &&\n> +\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n> +\n> +\tfree(base.ptr);\n> +\tfree(upstream.ptr);\n> +\tfree(branch.ptr);\n> +\tfree(result.ptr);\n> +\treturn ret;\n> +}\n\nThe function calls ll_merge() and passes the istate associated with\nthe_repository global, but have we read the index before the control\nreaches this point?  ll_merge() would want to consult custom low\nlevel merge from the attribute subsystem, and with istate that is\nnot populated, it would fail to learn what the in-index attributes\nsay, no?\n\nIf there is a reason why we want to ignore the attributes, perhaps\nwe should explicitly say so.\n\n"},{"id":"556997","messageId":"CAHwyqnUqkA+RzXNq4C0Lm0mK2-70YQkjnj9s_0E4M+7tGMN=fQ@mail.gmail.com","threadId":"66417","inReplyTo":"xmqq33tnvw58.fsf@gitster.g","subject":"Re: [PATCH v5 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-31T08:09:26Z","receivedAt":"2026-10-31T08:09:26Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > +static void find_landed_commits(struct landing_search *searches,\n> > +                             size_t searches_nr,\n> > +                             struct strmap *landed_commits)\n> > +{\n> > +     struct changed_paths cache;\n> > +     struct diff_options opt;\n>\n> A tiny nit, but because so many things are called x_option in our\n> codebase, I am used to seeing the variable also carry \"x\" to match,\n> or the use of mere \"opt\" down below would make us go back to see\n> what kind of option it refers to while reading the code.  I'd liked\n> to see this called \"diffopt\" instead.\n\nOk.\n\n\nHarald\n\n"},{"id":"556998","messageId":"CAHwyqnV5xv1jkpXggSi_HoDLJBTXCMqXzKR+ZaNLCBm-WmAduw@mail.gmail.com","threadId":"66417","inReplyTo":"xmqqwlqzuh1p.fsf@gitster.g","subject":"Re: [PATCH v5 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-31T08:10:07Z","receivedAt":"2026-10-31T08:10:07Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > +static int merge_keeps_upstream(const struct branch_change *change)\n> > +{\n> > +     mmfile_t base, upstream, branch;\n> > +     mmfile_t result = { 0 };\n> > +     int ret;\n> > +\n> > +     read_mmblob(&base, the_repository->objects, &change->base_oid);\n> > +     read_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n> > +     read_mmblob(&branch, the_repository->objects, &change->branch_oid);\n> > +     ret = ll_merge(&result, change->path, &base, \"base\",\n> > +                    &upstream, \"upstream\", &branch, \"branch\",\n> > +                    the_repository->index, NULL) == LL_MERGE_OK &&\n> > +           result.size == upstream.size &&\n> > +           !memcmp(result.ptr, upstream.ptr, upstream.size);\n> > +\n> > +     free(base.ptr);\n> > +     free(upstream.ptr);\n> > +     free(branch.ptr);\n> > +     free(result.ptr);\n> > +     return ret;\n> > +}\n>\n> The function calls ll_merge() and passes the istate associated with\n> the_repository global, but have we read the index before the control\n> reaches this point?  ll_merge() would want to consult custom low\n> level merge from the attribute subsystem, and with istate that is\n> not populated, it would fail to learn what the in-index attributes\n> say, no?\n>\n> If there is a reason why we want to ignore the attributes, perhaps\n> we should explicitly say so.\n\nI'll take a look.\n\n\nHarald\n\n"},{"id":"557062","messageId":"pull.2425.v6.git.git.1793521331.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v6 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-01T08:22:07Z","receivedAt":"2026-11-01T08:22:07Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v6:\n\n * Read the index before the file merge, so .gitattributes that are only in\n   the index are honored too.\n * Renamed the diff_options variables from opt to diffopt.\n\nChanges in v5:\n\n * git branch -h lists --delete-integrated in the usage lines\n * Giving both --delete-merged and --delete-integrated dies with a short\n   \"cannot be used together\" message instead of the full usage text\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-integrated <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  22 +-\n builtin/branch.c                 | 361 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 218 +++++++++++++++++++\n 4 files changed, 579 insertions(+), 30 deletions(-)\n\n\nbase-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v6\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v5:\n\n 1:  090aec5725 = 1:  e728b4ed0d branch: move the deleteMerged opt-out check into a helper\n 2:  24180bf4a2 = 2:  9858c8c81e branch: let delete_branches() name where a branch landed\n 3:  bf30f57ceb ! 3:  92bd9afda4 branch: add --delete-integrated to delete rebase and squash merged branches\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +static void collect_branch_changes(struct landing_search *search,\n      +\t\t\t\t   struct commit *base)\n      +{\n     -+\tstruct diff_options opt;\n     ++\tstruct diff_options diffopt;\n      +\n     -+\trepo_diff_setup(the_repository, &opt);\n     -+\topt.flags.recursive = 1;\n     -+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n     -+\tdiff_setup_done(&opt);\n     ++\trepo_diff_setup(the_repository, &diffopt);\n     ++\tdiffopt.flags.recursive = 1;\n     ++\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n     ++\tdiff_setup_done(&diffopt);\n      +\tdiff_tree_oid(get_commit_tree_oid(base),\n     -+\t\t      get_commit_tree_oid(search->rev), \"\", &opt);\n     ++\t\t      get_commit_tree_oid(search->rev), \"\", &diffopt);\n      +\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n      +\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n      +\t\tstruct branch_change *change;\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\t\t       &p->two->oid : null_oid(the_hash_algo));\n      +\t\tchange->branch_mode = p->two->mode;\n      +\t}\n     -+\tdiff_flush(&opt);\n     ++\tdiff_flush(&diffopt);\n      +}\n      +\n      +static int change_landed(const struct branch_change *change,\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +}\n      +\n      +static struct string_list *changed_paths_of(struct changed_paths *cache,\n     -+\t\t\t\t\t    struct diff_options *opt,\n     ++\t\t\t\t\t    struct diff_options *diffopt,\n      +\t\t\t\t\t    struct commit *commit)\n      +{\n      +\tstruct string_list **paths = changed_paths_at(cache, commit);\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\t\tdie(_(\"could not parse commit %s\"),\n      +\t\t    oid_to_hex(&parent->object.oid));\n      +\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n     -+\t\t      get_commit_tree_oid(commit), \"\", opt);\n     ++\t\t      get_commit_tree_oid(commit), \"\", diffopt);\n      +\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n      +\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n      +\n      +\t\tstring_list_append(*paths, p->two->path);\n      +\t}\n     -+\tdiff_flush(opt);\n     ++\tdiff_flush(diffopt);\n      +\tstring_list_sort(*paths);\n      +\treturn *paths;\n      +}\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\n      +static struct commit *find_landed_commit(struct landing_search *search,\n      +\t\t\t\t\t struct changed_paths *cache,\n     -+\t\t\t\t\t struct diff_options *opt)\n     ++\t\t\t\t\t struct diff_options *diffopt)\n      +{\n      +\tstruct commit *commit, *landed = NULL;\n      +\tstruct strvec args = STRVEC_INIT;\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\tif (prepare_revision_walk(&revs))\n      +\t\tdie(_(\"revision walk setup failed\"));\n      +\twhile (!landed && (commit = get_revision(&revs)))\n     -+\t\tif (touches_changes(changed_paths_of(cache, opt, commit),\n     ++\t\tif (touches_changes(changed_paths_of(cache, diffopt, commit),\n      +\t\t\t\t    search) &&\n      +\t\t    changes_landed(search, commit))\n      +\t\t\tlanded = commit;\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\t\t\t\tstruct strmap *landed_commits)\n      +{\n      +\tstruct changed_paths cache;\n     -+\tstruct diff_options opt;\n     ++\tstruct diff_options diffopt;\n      +\n      +\tfor (size_t i = 0; i < searches_nr; i++) {\n      +\t\tstruct landing_search *search = &searches[i];\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\t\tcommit_list_free(merge_bases);\n      +\t}\n      +\n     -+\trepo_diff_setup(the_repository, &opt);\n     -+\topt.flags.recursive = 1;\n     -+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n     -+\topt.no_free = 1;\n     -+\tdiff_setup_done(&opt);\n     ++\trepo_diff_setup(the_repository, &diffopt);\n     ++\tdiffopt.flags.recursive = 1;\n     ++\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n     ++\tdiffopt.no_free = 1;\n     ++\tdiff_setup_done(&diffopt);\n      +\tinit_changed_paths(&cache);\n      +\n      +\tfor (size_t i = 0; i < searches_nr; i++) {\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n      +\n      +\t\tif (!searches[i].changes_nr)\n      +\t\t\tcontinue;\n     -+\t\tlanded = find_landed_commit(&searches[i], &cache, &opt);\n     ++\t\tlanded = find_landed_commit(&searches[i], &cache, &diffopt);\n      +\t\tif (landed)\n      +\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n      +\t\t\t\t   landed);\n      +\t}\n      +\n      +\tdeep_clear_changed_paths(&cache, free_changed_paths);\n     -+\topt.no_free = 0;\n     -+\tdiff_free(&opt);\n     ++\tdiffopt.no_free = 0;\n     ++\tdiff_free(&diffopt);\n      +}\n      +\n       static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n 4:  9443055242 ! 4:  006a57d7d1 branch: let --delete-integrated find branches merged onto changed files\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n       \t\t       &p->two->oid : null_oid(the_hash_algo));\n       \t\tchange->branch_mode = p->two->mode;\n      @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *search,\n     - \tdiff_flush(&opt);\n     + \tdiff_flush(&diffopt);\n       }\n       \n      -static int change_landed(const struct branch_change *change,\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n      +\tmmfile_t result = { 0 };\n      +\tint ret;\n      +\n     ++\trepo_read_index(the_repository);\n      +\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n      +\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n      +\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n\n-- \ngitgitgadget\n\n"},{"id":"557063","messageId":"e728b4ed0d7e9f72f88969bed9c4fc1f8ceec836.1793521331.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v6.git.git.1793521331.gitgitgadget@gmail.com","subject":"[PATCH v6 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-01T08:22:08Z","receivedAt":"2026-11-01T08:22:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..171b13b081 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"557064","messageId":"9858c8c81e9c58bcd5d3dc55c65bbc70129a55c4.1793521331.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v6.git.git.1793521331.gitgitgadget@gmail.com","subject":"[PATCH v6 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-01T08:22:09Z","receivedAt":"2026-11-01T08:22:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 171b13b081..99b0dd45e9 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"557065","messageId":"92bd9afda49e429d2c958824af532f7ae57998b7.1793521331.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v6.git.git.1793521331.gitgitgadget@gmail.com","subject":"[PATCH v6 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-01T08:22:10Z","receivedAt":"2026-11-01T08:22:10Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 253 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 176 +++++++++++++++++++++\n 4 files changed, 441 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..6be28c8a4e 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 99b0dd45e9..818c916791 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -41,6 +47,8 @@ static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n \t   \"[<branch-pattern>...]\"),\n+\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n+\t   \"[<branch-pattern>...]\"),\n \tNULL\n };\n \n@@ -201,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +853,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options diffopt;\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&diffopt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&diffopt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *diffopt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(diffopt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *diffopt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, diffopt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options diffopt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiffopt.no_free = 1;\n+\tdiff_setup_done(&diffopt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &diffopt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\tdiffopt.no_free = 0;\n+\tdiff_free(&diffopt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1057,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1094,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1138,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1146,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1225,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1283,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1336,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1116,14 +1344,18 @@ int cmd_branch(int argc,\n \t    filter.points_at.nr || filter.forked.nr)\n \t\tlist = 1;\n \n+\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n+\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n+\n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1400,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1340,6 +1578,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..1fbb173ad4 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1925,6 +1925,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -1979,6 +2012,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2178,4 +2348,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"557066","messageId":"006a57d7d150d581c920e2d2933bd9d9faa43eca.1793521331.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v6.git.git.1793521331.gitgitgadget@gmail.com","subject":"[PATCH v6 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-01T08:22:11Z","receivedAt":"2026-11-01T08:22:11Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services, where the \"Squash and merge\" button on GitHub\nlands pull requests this way while the main branch keeps moving. A\nrebase merge onto such changes looks the same.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 13 ++++---\n builtin/branch.c              | 72 ++++++++++++++++++++++++++++++-----\n t/t3200-branch.sh             | 42 ++++++++++++++++++++\n 3 files changed, 112 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 6be28c8a4e..489a9d5fd0 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,14 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it, or when merging the branch's version into the\n+\tcommit's leaves it unchanged, which reads the file and may\n+\tdownload blobs in a partial clone. The same rules for which\n+\tbranches are kept apply as for `--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 818c916791..477b4a92c4 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -853,10 +855,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -888,6 +897,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -895,25 +906,66 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&diffopt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\trepo_read_index(the_repository);\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 1fbb173ad4..d4ebbd0ae9 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2122,6 +2122,48 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"558164","messageId":"CAHwyqnXuN6vjBD4_A7FsSHTvra_k7XCpZPdm=DSdH+r+mNgekA@mail.gmail.com","threadId":"66417","inReplyTo":"006a57d7d150d581c920e2d2933bd9d9faa43eca.1793521331.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v6 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-11-11T08:30:09Z","receivedAt":"2026-11-11T08:30:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Could I get some review here please?\n\n\nHarald\n\n"},{"id":"558222","messageId":"20261111152753.99845-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"CAHwyqnXuN6vjBD4_A7FsSHTvra_k7XCpZPdm=DSdH+r+mNgekA@mail.gmail.com","subject":"Re: [PATCH v6 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-11-11T15:27:53Z","receivedAt":"2026-11-11T15:27:53Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"Hi Harald\n\nOn Sat, Nov 1, 2026 Harald Nordgren via GitGitGadget wrote:\n> +static int merge_keeps_upstream(const struct branch_change *change)\n> +{\n> +\tmmfile_t base, upstream, branch;\n> +\tmmfile_t result = { 0 };\n> +\tint ret;\n> +\n> +\trepo_read_index(the_repository);\n\nThis runs once for each changed file at each candidate commit. After\nthe first call, read_index_from() returns early, but repo_read_index()\nstill calls clear_skip_worktree_from_present_files() every time. In a\nsparse checkout, that walks the index and can lstat() the\nskip-worktree paths again for each merge. Could the index be read once,\nbefore the walk in find_landed_commits()?\n\nThis also answers only half of Junio's question on v5. The attributes\nnow come from the index of the current worktree, not from the upstream\ncommit. A merge driver that is set only upstream is ignored, and in a\nbare repository the index is empty, so no attributes apply. That can\nbe the right choice, but I think the commit message should say so.\n\n> +test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n\nAll the tests for the new code path expect a deletion. I added these\ncases locally on top of v6, and all of them keep the branch, as\nexpected:\n\n - upstream changed other lines of the file, but the branch was never\n   merged\n - upstream has only one of the two hunks of the branch\n - upstream changed the same line in a different way, so the merge\n   conflicts\n\nA test where the merge does not leave upstream unchanged would protect\nthe behaviour that matters most here, which is to keep work that did\nnot land. I am happy to send the tests if they help.\n\nWith those points, the patch looks good to me.\n\nThanks\nJasper\n\n"},{"id":"558448","messageId":"CAHwyqnUjFybso5C44kjDpgFYgsct=u9F1gACQLYEwF6Mcn6Y4A@mail.gmail.com","threadId":"66417","inReplyTo":"20261111152753.99845-1-me@jaspermayone.com","subject":"Re: [PATCH v6 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-11-14T11:08:41Z","receivedAt":"2026-11-14T11:08:41Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> A test where the merge does not leave upstream unchanged would protect\n> the behaviour that matters most here, which is to keep work that did\n> not land. I am happy to send the tests if they help.\n\nYes, please send them over!\n\n> With those points, the patch looks good to me.\n\nThanks!\n\n"},{"id":"558451","messageId":"pull.2425.v7.git.git.1794662670.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v7 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T13:24:26Z","receivedAt":"2026-11-14T13:24:26Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v7:\n\n * Added a test that --delete-integrated keeps a branch whose changes did\n   not land.\n * Read the index once before the walk in find_landed_commits(), instead of\n   for every file merge at every candidate commit.\n * The commit message now says the merge uses this repository's attributes\n   from the working tree and the index.\n\nChanges in v6:\n\n * Read the index before the file merge, so .gitattributes that are only in\n   the index are honored too.\n * Renamed the diff_options variables from opt to diffopt.\n\nChanges in v5:\n\n * git branch -h lists --delete-integrated in the usage lines\n * Giving both --delete-merged and --delete-integrated dies with a short\n   \"cannot be used together\" message instead of the full usage text\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-integrated <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  22 +-\n builtin/branch.c                 | 361 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 271 +++++++++++++++++++++++\n 4 files changed, 632 insertions(+), 30 deletions(-)\n\n\nbase-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v7\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v6:\n\n 1:  e728b4ed0d = 1:  25d2441ce7 branch: move the deleteMerged opt-out check into a helper\n 2:  9858c8c81e = 2:  18c3edb6e3 branch: let delete_branches() name where a branch landed\n 3:  92bd9afda4 = 3:  7bc634c794 branch: add --delete-integrated to delete rebase and squash merged branches\n 4:  006a57d7d1 ! 4:  4b5485584e branch: let --delete-integrated find branches merged onto changed files\n     @@ Commit message\n          unchanged. This has to read the files, so in a partial clone it may\n          download blobs.\n      \n     +    The merge uses the attributes of this repository, from the working tree\n     +    and the index, the same as a merge run here would. A merge driver that\n     +    only the upstream repository has is not seen. In a bare repository only\n     +    info/attributes and the global file apply.\n     +\n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/git-branch.adoc ##\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n      +\tmmfile_t result = { 0 };\n      +\tint ret;\n      +\n     -+\trepo_read_index(the_repository);\n      +\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n      +\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n      +\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n       \treturn 1;\n       }\n       \n     +@@ builtin/branch.c: static void find_landed_commits(struct landing_search *searches,\n     + \tdiffopt.no_free = 1;\n     + \tdiff_setup_done(&diffopt);\n     + \tinit_changed_paths(&cache);\n     ++\trepo_read_index(the_repository);\n     + \n     + \tfor (size_t i = 0; i < searches_nr; i++) {\n     + \t\tstruct commit *landed;\n      \n       ## t/t3200-branch.sh ##\n      @@ t/t3200-branch.sh: test_expect_success '--delete-integrated deletes a squash merged branch that was\n     @@ t/t3200-branch.sh: test_expect_success '--delete-integrated deletes a squash mer\n      +\t\tEOF\n      +\t)\n      +'\n     ++\n     ++test_expect_success '--delete-integrated keeps branches whose changes did not land' '\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >doc &&\n     ++\t\tgit add doc &&\n     ++\t\tgit commit -m \"add doc\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\tsetup_repo_for_delete_merged &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit checkout -b unmerged --track origin/next &&\n     ++\t\ttest_write_lines 1 2 3 4 5 6 7 eight 9 10 >doc &&\n     ++\t\tgit commit -a -m \"edit line 8\" &&\n     ++\t\tgit push origin unmerged &&\n     ++\t\tgit checkout -b half-landed --track origin/next &&\n     ++\t\ttest_write_lines one 2 3 4 5 6 7 8 9 ten >doc &&\n     ++\t\tgit commit -a -m \"edit lines 1 and 10\" &&\n     ++\t\tgit push origin half-landed &&\n     ++\t\tgit checkout -b conflicting --track origin/next &&\n     ++\t\ttest_write_lines 1 2 three 4 5 6 7 8 9 10 >doc &&\n     ++\t\tgit commit -a -m \"edit line 3\" &&\n     ++\t\tgit push origin conflicting &&\n     ++\t\tgit checkout --detach\n     ++\t) &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >doc &&\n     ++\t\tgit commit -a -m \"edit line 5\" &&\n     ++\t\ttest_write_lines one 2 3 4 five 6 7 8 9 10 >doc &&\n     ++\t\tgit commit -a -m \"take line 1 from half-landed\" &&\n     ++\t\ttest_write_lines one 2 THREE 4 five 6 7 8 9 10 >doc &&\n     ++\t\tgit commit -a -m \"edit line 3 differently\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n     ++\t\ttest_must_be_empty actual &&\n     ++\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\tconflicting\n     ++\t\thalf-landed\n     ++\t\tmain\n     ++\t\tunmerged\n     ++\t\tEOF\n     ++\t)\n     ++'\n      +\n       test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n       \tsetup_repo_for_delete_merged &&\n\n-- \ngitgitgadget\n\n"},{"id":"558452","messageId":"25d2441ce75e0794e8df4d2b6d6c4b6d50902174.1794662670.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v7.git.git.1794662670.gitgitgadget@gmail.com","subject":"[PATCH v7 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T13:24:27Z","receivedAt":"2026-11-14T13:24:27Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b536b619bb..2c1a8b8395 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"558453","messageId":"18c3edb6e3a12a1e82891728b0c7e3b189b3457a.1794662670.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v7.git.git.1794662670.gitgitgadget@gmail.com","subject":"[PATCH v7 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T13:24:28Z","receivedAt":"2026-11-14T13:24:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 2c1a8b8395..6e6b04b11d 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"558454","messageId":"7bc634c794985175d32820b30e5e0397a048e1d1.1794662670.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v7.git.git.1794662670.gitgitgadget@gmail.com","subject":"[PATCH v7 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T13:24:29Z","receivedAt":"2026-11-14T13:24:29Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 253 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 176 +++++++++++++++++++++\n 4 files changed, 441 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..6be28c8a4e 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 6e6b04b11d..ee92e71f51 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -41,6 +47,8 @@ static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n \t   \"[<branch-pattern>...]\"),\n+\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n+\t   \"[<branch-pattern>...]\"),\n \tNULL\n };\n \n@@ -201,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +853,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options diffopt;\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&diffopt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&diffopt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *diffopt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(diffopt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *diffopt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, diffopt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options diffopt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiffopt.no_free = 1;\n+\tdiff_setup_done(&diffopt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &diffopt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\tdiffopt.no_free = 0;\n+\tdiff_free(&diffopt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1057,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1094,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1138,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1146,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1225,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1283,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1336,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1116,14 +1344,18 @@ int cmd_branch(int argc,\n \t    filter.points_at.nr || filter.forked.nr)\n \t\tlist = 1;\n \n+\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n+\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n+\n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1400,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1347,6 +1585,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 44336eef81..54461e90b3 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1963,6 +1963,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -2017,6 +2050,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2216,4 +2386,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"558455","messageId":"4b5485584e218e6136835c02a4e9b01ef9a5c574.1794662670.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v7.git.git.1794662670.gitgitgadget@gmail.com","subject":"[PATCH v7 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T13:24:30Z","receivedAt":"2026-11-14T13:24:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services, where the \"Squash and merge\" button on GitHub\nlands pull requests this way while the main branch keeps moving. A\nrebase merge onto such changes looks the same.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nThe merge uses the attributes of this repository, from the working tree\nand the index, the same as a merge run here would. A merge driver that\nonly the upstream repository has is not seen. In a bare repository only\ninfo/attributes and the global file apply.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 13 +++--\n builtin/branch.c              | 72 ++++++++++++++++++++++----\n t/t3200-branch.sh             | 95 +++++++++++++++++++++++++++++++++++\n 3 files changed, 165 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 6be28c8a4e..489a9d5fd0 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,14 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it, or when merging the branch's version into the\n+\tcommit's leaves it unchanged, which reads the file and may\n+\tdownload blobs in a partial clone. The same rules for which\n+\tbranches are kept apply as for `--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex ee92e71f51..8547668fc4 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -853,10 +855,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -888,6 +897,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -895,25 +906,65 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&diffopt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \n@@ -1015,6 +1066,7 @@ static void find_landed_commits(struct landing_search *searches,\n \tdiffopt.no_free = 1;\n \tdiff_setup_done(&diffopt);\n \tinit_changed_paths(&cache);\n+\trepo_read_index(the_repository);\n \n \tfor (size_t i = 0; i < searches_nr; i++) {\n \t\tstruct commit *landed;\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 54461e90b3..088f029e72 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2160,6 +2160,101 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated keeps branches whose changes did not land' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >doc &&\n+\t\tgit add doc &&\n+\t\tgit commit -m \"add doc\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unmerged --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 eight 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 8\" &&\n+\t\tgit push origin unmerged &&\n+\t\tgit checkout -b half-landed --track origin/next &&\n+\t\ttest_write_lines one 2 3 4 5 6 7 8 9 ten >doc &&\n+\t\tgit commit -a -m \"edit lines 1 and 10\" &&\n+\t\tgit push origin half-landed &&\n+\t\tgit checkout -b conflicting --track origin/next &&\n+\t\ttest_write_lines 1 2 three 4 5 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3\" &&\n+\t\tgit push origin conflicting &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\ttest_write_lines one 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"take line 1 from half-landed\" &&\n+\t\ttest_write_lines one 2 THREE 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3 differently\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tconflicting\n+\t\thalf-landed\n+\t\tmain\n+\t\tunmerged\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"558469","messageId":"20261114171437.DqB_k%taahol@utu.fi","threadId":"66417","inReplyTo":"4b5485584e218e6136835c02a4e9b01ef9a5c574.1794662670.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v7 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Tuomas Ahola","fromEmail":"taahol@utu.fi","sentAt":"2026-11-14T17:14:37Z","receivedAt":"2026-11-14T17:14:37Z","isPatch":true,"sender":{"key":"taahol@utu.fi","avatar":"https://avatars.githubusercontent.com/u/114303477?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> wrote:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> A branch that upstream integrated with\n> \n>     git merge --squash topic\n>     git commit\n> \n> after upstream had also changed the files the branch touches is still\n> kept by \"git branch --delete-integrated\". The squash commit has neither\n> the branch's version of those files nor the fork point's, so the\n> previous check cannot see that the work landed. This happens all the\n> time on hosting services, where the \"Squash and merge\" button on GitHub\n\nNit: that sentence scans a bit awkwardly.  Perhaps something along these\nlines would make the idea more clear?\n\n\tThis happens all the time on hosting services such as GitHub,\n\twhere the \"Squash and merge\" button lands...\n\n> lands pull requests this way while the main branch keeps moving. A\n\nHmm, I'm not sure if I understood that.  What is \"while the main branch\nkeeps moving\" trying to say?\n\n> rebase merge onto such changes looks the same.\n> \n> For each such file, merge the branch's version into the upstream one\n> and treat the change as landed when that leaves the upstream version\n> unchanged. This has to read the files, so in a partial clone it may\n> download blobs.\n> \n> The merge uses the attributes of this repository, from the working tree\n> and the index, the same as a merge run here would. A merge driver that\n> only the upstream repository has is not seen. In a bare repository only\n> info/attributes and the global file apply.\n> \n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  Documentation/git-branch.adoc | 13 +++--\n>  builtin/branch.c              | 72 ++++++++++++++++++++++----\n>  t/t3200-branch.sh             | 95 +++++++++++++++++++++++++++++++++++\n>  3 files changed, 165 insertions(+), 15 deletions(-)\n> \n> diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\n> index 6be28c8a4e..489a9d5fd0 100644\n> --- a/Documentation/git-branch.adoc\n> +++ b/Documentation/git-branch.adoc\n> @@ -240,11 +240,14 @@ remove it anyway.\n>  \tLike `--delete-merged`, but also delete a branch whose changes\n>  \tthe upstream already has even though its commits are not there,\n>  \tas happens after a squash or rebase merge. Such a branch is\n> -\tdeleted when some upstream commit since the fork point has its\n> -\tversion of every file it changed, even if those changes were\n> -\tlater reverted, and the message names the first such commit.\n> -\tIt does not matter who made that commit. The same rules for\n> -\twhich branches are kept apply as for `--delete-merged`.\n> +\tdeleted when some upstream commit since the fork point has all\n> +\tof its changes, even if they were later reverted, and the\n> +\tmessage names the first such commit. It does not matter who made\n> +\tthat commit. A file counts when the commit has the branch's\n> +\tversion of it, or when merging the branch's version into the\n> +\tcommit's leaves it unchanged, which reads the file and may\n\nErr, what exactly is the antedecent of that \"which\"?  (Okay, I think the answer\nis \"merging\", but I had to read the sentence again and again before I saw that.\nCan we make the passage a little easier to grasp?)\n\n> +\tdownload blobs in a partial clone. The same rules for which\n> +\tbranches are kept apply as for `--delete-merged`.\n>  \n>  `--dry-run`::\n>  \tWith `--delete-merged` or `--delete-integrated`, print which\n> diff --git a/builtin/branch.c b/builtin/branch.c\n\n"},{"id":"558559","messageId":"20261116161603.57379-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"4b5485584e218e6136835c02a4e9b01ef9a5c574.1794662670.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v7 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-11-16T16:16:03Z","receivedAt":"2026-11-16T16:16:03Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"Hi Harald\n\nOn v6 you asked me to send my tests over. The new test in v7,\n\"--delete-integrated keeps branches whose changes did not land\",\ncovers the same three cases I had: a branch that never landed, a\nbranch with only part of its change upstream, and a branch whose\nchange conflicts with upstream. So there is no need for mine.\n\nI built v7 and ran t3200 with my four tests added on top. All 209\ntests pass. v7 reads the index once in find_landed_commits(), and\nthe commit message now says where the attributes come from, so all\nthree of my points are addressed.\n\nTested-by: Jasper Mayone <me@jaspermayone.com>\n\nThanks\nJasper\n\n"},{"id":"558616","messageId":"pull.2425.v8.git.git.1794921876.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v8 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-17T13:24:32Z","receivedAt":"2026-11-17T13:24:32Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This treats such a branch as merged when some\nupstream commit contains all of its changes, and names that commit in the\noutput:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v8:\n\n * Reword commit messages.\n\nChanges in v7:\n\n * Added a test that --delete-integrated keeps a branch whose changes did\n   not land.\n * Read the index once before the walk in find_landed_commits(), instead of\n   for every file merge at every candidate commit.\n * The commit message now says the merge uses this repository's attributes\n   from the working tree and the index.\n\nChanges in v6:\n\n * Read the index before the file merge, so .gitattributes that are only in\n   the index are honored too.\n * Renamed the diff_options variables from opt to diffopt.\n\nChanges in v5:\n\n * git branch -h lists --delete-integrated in the usage lines\n * Giving both --delete-merged and --delete-integrated dies with a short\n   \"cannot be used together\" message instead of the full usage text\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-integrated <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  23 +-\n builtin/branch.c                 | 361 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 271 +++++++++++++++++++++++\n 4 files changed, 633 insertions(+), 30 deletions(-)\n\n\nbase-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v8\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v8\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v7:\n\n 1:  25d2441ce7 = 1:  25d2441ce7 branch: move the deleteMerged opt-out check into a helper\n 2:  18c3edb6e3 = 2:  18c3edb6e3 branch: let delete_branches() name where a branch landed\n 3:  7bc634c794 = 3:  7bc634c794 branch: add --delete-integrated to delete rebase and squash merged branches\n 4:  4b5485584e ! 4:  22658105ed branch: let --delete-integrated find branches merged onto changed files\n     @@ Commit message\n          kept by \"git branch --delete-integrated\". The squash commit has neither\n          the branch's version of those files nor the fork point's, so the\n          previous check cannot see that the work landed. This happens all the\n     -    time on hosting services, where the \"Squash and merge\" button on GitHub\n     -    lands pull requests this way while the main branch keeps moving. A\n     -    rebase merge onto such changes looks the same.\n     +    time on hosting services such as GitHub, where the \"Squash and merge\"\n     +    button lands pull requests this way, and other pull requests often\n     +    touch the same files first. A rebase merge onto such changes looks the\n     +    same.\n      \n          For each such file, merge the branch's version into the upstream one\n          and treat the change as landed when that leaves the upstream version\n     @@ Documentation/git-branch.adoc: remove it anyway.\n      +\tof its changes, even if they were later reverted, and the\n      +\tmessage names the first such commit. It does not matter who made\n      +\tthat commit. A file counts when the commit has the branch's\n     -+\tversion of it, or when merging the branch's version into the\n     -+\tcommit's leaves it unchanged, which reads the file and may\n     -+\tdownload blobs in a partial clone. The same rules for which\n     -+\tbranches are kept apply as for `--delete-merged`.\n     ++\tversion of it. It also counts when merging the branch's version\n     ++\tinto the commit's version leaves the commit's version unchanged.\n     ++\tThat merge reads the file, and in a partial clone it may download\n     ++\tblobs. The same rules for which branches are kept apply as for\n     ++\t`--delete-merged`.\n       \n       `--dry-run`::\n       \tWith `--delete-merged` or `--delete-integrated`, print which\n\n-- \ngitgitgadget\n\n"},{"id":"558617","messageId":"25d2441ce75e0794e8df4d2b6d6c4b6d50902174.1794921876.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v8.git.git.1794921876.gitgitgadget@gmail.com","subject":"[PATCH v8 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-17T13:24:33Z","receivedAt":"2026-11-17T13:24:33Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b536b619bb..2c1a8b8395 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"558618","messageId":"18c3edb6e3a12a1e82891728b0c7e3b189b3457a.1794921876.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v8.git.git.1794921876.gitgitgadget@gmail.com","subject":"[PATCH v8 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-17T13:24:34Z","receivedAt":"2026-11-17T13:24:34Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 2c1a8b8395..6e6b04b11d 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"558619","messageId":"7bc634c794985175d32820b30e5e0397a048e1d1.1794921876.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v8.git.git.1794921876.gitgitgadget@gmail.com","subject":"[PATCH v8 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-17T13:24:35Z","receivedAt":"2026-11-17T13:24:35Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 253 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 176 +++++++++++++++++++++\n 4 files changed, 441 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex bfdf459329..6be28c8a4e 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 6e6b04b11d..ee92e71f51 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -41,6 +47,8 @@ static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n \t   \"[<branch-pattern>...]\"),\n+\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n+\t   \"[<branch-pattern>...]\"),\n \tNULL\n };\n \n@@ -201,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +853,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options diffopt;\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&diffopt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&diffopt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *diffopt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(diffopt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *diffopt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, diffopt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options diffopt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiffopt.no_free = 1;\n+\tdiff_setup_done(&diffopt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &diffopt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\tdiffopt.no_free = 0;\n+\tdiff_free(&diffopt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1057,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1094,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1138,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1146,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1225,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1283,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1336,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1116,14 +1344,18 @@ int cmd_branch(int argc,\n \t    filter.points_at.nr || filter.forked.nr)\n \t\tlist = 1;\n \n+\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n+\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n+\n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1400,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1347,6 +1585,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 44336eef81..54461e90b3 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1963,6 +1963,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -2017,6 +2050,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2216,4 +2386,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"558620","messageId":"22658105edeacf296a18373400e7b35141e053ef.1794921876.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v8.git.git.1794921876.gitgitgadget@gmail.com","subject":"[PATCH v8 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-17T13:24:36Z","receivedAt":"2026-11-17T13:24:36Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services such as GitHub, where the \"Squash and merge\"\nbutton lands pull requests this way, and other pull requests often\ntouch the same files first. A rebase merge onto such changes looks the\nsame.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nThe merge uses the attributes of this repository, from the working tree\nand the index, the same as a merge run here would. A merge driver that\nonly the upstream repository has is not seen. In a bare repository only\ninfo/attributes and the global file apply.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc | 14 ++++--\n builtin/branch.c              | 72 ++++++++++++++++++++++----\n t/t3200-branch.sh             | 95 +++++++++++++++++++++++++++++++++++\n 3 files changed, 166 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex 6be28c8a4e..ec468fcb8d 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,15 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it. It also counts when merging the branch's version\n+\tinto the commit's version leaves the commit's version unchanged.\n+\tThat merge reads the file, and in a partial clone it may download\n+\tblobs. The same rules for which branches are kept apply as for\n+\t`--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex ee92e71f51..8547668fc4 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -853,10 +855,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n \tunsigned short branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -888,6 +897,8 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n \t\tchange->branch_mode = p->two->mode;\n@@ -895,25 +906,65 @@ static void collect_branch_changes(struct landing_search *search,\n \tdiff_flush(&diffopt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \n@@ -1015,6 +1066,7 @@ static void find_landed_commits(struct landing_search *searches,\n \tdiffopt.no_free = 1;\n \tdiff_setup_done(&diffopt);\n \tinit_changed_paths(&cache);\n+\trepo_read_index(the_repository);\n \n \tfor (size_t i = 0; i < searches_nr; i++) {\n \t\tstruct commit *landed;\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 54461e90b3..088f029e72 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2160,6 +2160,101 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated keeps branches whose changes did not land' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >doc &&\n+\t\tgit add doc &&\n+\t\tgit commit -m \"add doc\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unmerged --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 eight 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 8\" &&\n+\t\tgit push origin unmerged &&\n+\t\tgit checkout -b half-landed --track origin/next &&\n+\t\ttest_write_lines one 2 3 4 5 6 7 8 9 ten >doc &&\n+\t\tgit commit -a -m \"edit lines 1 and 10\" &&\n+\t\tgit push origin half-landed &&\n+\t\tgit checkout -b conflicting --track origin/next &&\n+\t\ttest_write_lines 1 2 three 4 5 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3\" &&\n+\t\tgit push origin conflicting &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\ttest_write_lines one 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"take line 1 from half-landed\" &&\n+\t\ttest_write_lines one 2 THREE 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3 differently\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tconflicting\n+\t\thalf-landed\n+\t\tmain\n+\t\tunmerged\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"558767","messageId":"xmqqik1t2119.fsf@gitster.g","threadId":"66417","inReplyTo":"22658105edeacf296a18373400e7b35141e053ef.1794921876.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v8 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-18T23:05:38Z","receivedAt":"2026-11-18T23:05:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> @@ -888,6 +897,8 @@ static void collect_branch_changes(struct landing_search *search,\n>  \t\t\t   search->changes_alloc);\n>  \t\tchange = &search->changes[search->changes_nr++];\n>  \t\tchange->path = xstrdup(p->two->path);\n> +\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n> +\t\t       &p->one->oid : null_oid(the_hash_algo));\n>  \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n>  \t\t       &p->two->oid : null_oid(the_hash_algo));\n>  \t\tchange->branch_mode = p->two->mode;\n\nMental note.  We are recording the mode in the \"branch\", but never\nrecord the mode of the path in the base here.\n\n> @@ -895,25 +906,65 @@ static void collect_branch_changes(struct landing_search *search,\n>  \tdiff_flush(&diffopt);\n>  }\n>  \n> +static int merge_keeps_upstream(const struct branch_change *change)\n> +{\n> +\tmmfile_t base, upstream, branch;\n> +\tmmfile_t result = { 0 };\n> +\tint ret;\n> +\n> +\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n> +\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n> +\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n\nImagine that a path that originally was a gitlink has been replaced\nwith different regular files on our branch and the upstream.  If\nthis function ever gets called on such a path from changes_landed(),\nthe base_oid would record the submodule's commit object name, and\nread_mmblob() would probably crash.\n\n> +\tret = ll_merge(&result, change->path, &base, \"base\",\n> +\t\t       &upstream, \"upstream\", &branch, \"branch\",\n> +\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n> +\t      result.size == upstream.size &&\n> +\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n> +\n> +\tfree(base.ptr);\n> +\tfree(upstream.ptr);\n> +\tfree(branch.ptr);\n> +\tfree(result.ptr);\n> +\treturn ret;\n> +}\n> +\n> +static enum change_state change_state_at(struct branch_change *change,\n> +\t\t\t\t\t struct commit *commit)\n>  {\n>  \tunsigned short mode;\n>  \n>  \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n> +\t\t\t   change->path, &change->upstream_oid, &mode))\n> +\t\treturn is_null_oid(&change->branch_oid) ?\n> +\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n> +\tif (oideq(&change->upstream_oid, &change->branch_oid))\n> +\t\treturn mode == change->branch_mode ?\n> +\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n>\n> +\tif (is_null_oid(&change->base_oid) ||\n> +\t    is_null_oid(&change->branch_oid) ||\n> +\t    oideq(&change->upstream_oid, &change->base_oid) ||\n> +\t    mode != change->branch_mode || !S_ISREG(mode))\n> +\t\treturn CHANGE_MISSING;\n> +\treturn CHANGE_NEEDS_MERGE;\n>  }\n\nNow, if a gitlink was turned into different regular files, as object\nnames in upstream/branch/base are all different, none of them is\nnull_oid, and as we saw earlier, branch_mode is from p->two->mode\nfound in collect_branch_changes(), and mode would also be for the\nregular file.  As the mode is the same, we end up returning\nCHANGE_NEEDS_NERGE from here.  Which would cause ...\n\n>  static int changes_landed(struct landing_search *search,\n>  \t\t\t  struct commit *commit)\n>  {\n> -\tfor (size_t i = 0; i < search->changes_nr; i++)\n> -\t\tif (!change_landed(&search->changes[i], commit))\n> +\tfor (size_t i = 0; i < search->changes_nr; i++) {\n> +\t\tstruct branch_change *change = &search->changes[i];\n> +\n> +\t\tchange->state = change_state_at(change, commit);\n> +\t\tif (change->state == CHANGE_MISSING)\n>  \t\t\treturn 0;\n> +\t}\n> +\tfor (size_t i = 0; i < search->changes_nr; i++) {\n> +\t\tstruct branch_change *change = &search->changes[i];\n> +\n> +\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n> +\t\t    !merge_keeps_upstream(change))\n> +\t\t\treturn 0;\n> +\t}\n\n... this code to call merge_keeps_upstream(), causing read_mmfile()\non the gitlink commit from submodule object database, leading to a\ncrash, no?\n\nOr am I misreading the code flow?\n\n>  \treturn 1;\n>  }\n\n"},{"id":"558851","messageId":"20261119151436.86191-1-me@jaspermayone.com","threadId":"66417","inReplyTo":"xmqqik1t2119.fsf@gitster.g","subject":"Re: [PATCH v8 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Jasper Mayone","fromEmail":"me@jaspermayone.com","sentAt":"2026-11-19T15:14:36Z","receivedAt":"2026-11-19T15:14:36Z","isPatch":true,"sender":{"key":"me@jaspermayone.com","avatar":null},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> ... this code to call merge_keeps_upstream(), causing read_mmfile()\n> on the gitlink commit from submodule object database, leading to a\n> crash, no?\n>\n> Or am I misreading the code flow?\n\nYou are reading it right. I tried it on v8: the upstream has \"lib\" as\na gitlink, the branch replaces it with a regular file, and the\nupstream replaces it with a different regular file. Then\n\n  $ git branch --delete-integrated origin/main\n  fatal: unable to read blob object dae1aadd914ecfe1e09b3adb3c15abdd0085e238\n\nIt does not segfault, because read_mmblob() checks the object type\nand dies, but the command stops and no branch is deleted.\n\nRecording the mode of the base and requiring a regular file there too\nfixes it for me. The branch is then kept, and t3200 still passes:\n\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -864,7 +864,7 @@ enum change_state {\n struct branch_change {\n \tchar *path;\n \tstruct object_id base_oid, branch_oid, upstream_oid;\n-\tunsigned short branch_mode;\n+\tunsigned short base_mode, branch_mode;\n \tenum change_state state;\n };\n \n@@ -901,6 +901,7 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->base_mode = p->one->mode;\n \t\tchange->branch_mode = p->two->mode;\n \t}\n \tdiff_flush(&diffopt);\n@@ -943,7 +944,8 @@ static enum change_state change_state_at(struct branch_change *change,\n \tif (is_null_oid(&change->base_oid) ||\n \t    is_null_oid(&change->branch_oid) ||\n \t    oideq(&change->upstream_oid, &change->base_oid) ||\n-\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t    mode != change->branch_mode || !S_ISREG(mode) ||\n+\t    !S_ISREG(change->base_mode))\n \t\treturn CHANGE_MISSING;\n \treturn CHANGE_NEEDS_MERGE;\n }\n\nHarald, a test for this case in t3200 would be good too.\n\nThanks\nJasper\n\n"},{"id":"558939","messageId":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.git.git.1790667030497.gitgitgadget@gmail.com","subject":"[PATCH v9 0/4] branch: let --delete-merged find squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-20T14:24:20Z","receivedAt":"2026-11-20T14:24:20Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Branches merged on GitHub with \"Squash and merge\" or \"Rebase and merge\" are\nnever deleted by git branch --delete-merged, because their tips are not\nreachable from the upstream. This adds git branch --delete-integrated\n<pattern>, which works like --delete-merged and also deletes such a branch\nwhen some upstream commit contains all of its changes. It names that commit\nin the output:\n\nDeleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\n\nChanges in v9:\n\n * Fix --delete-integrated dying with \"unable to read blob object\" when a\n   path was a submodule at the fork point and both the branch and upstream\n   replaced it with different regular files. Such a branch is now kept.\n * Add a test for that case.\n\nChanges in v8:\n\n * Reword commit messages.\n\nChanges in v7:\n\n * Added a test that --delete-integrated keeps a branch whose changes did\n   not land.\n * Read the index once before the walk in find_landed_commits(), instead of\n   for every file merge at every candidate commit.\n * The commit message now says the merge uses this repository's attributes\n   from the working tree and the index.\n\nChanges in v6:\n\n * Read the index before the file merge, so .gitattributes that are only in\n   the index are honored too.\n * Renamed the diff_options variables from opt to diffopt.\n\nChanges in v5:\n\n * git branch -h lists --delete-integrated in the usage lines\n * Giving both --delete-merged and --delete-integrated dies with a short\n   \"cannot be used together\" message instead of the full usage text\n\nChanges in v4:\n\n * --delete-merged works as it does on master again: it only deletes\n   branches whose tip is reachable from their upstream\n * New --delete-integrated <pattern> also deletes branches that were rebase\n   or squash merged, including those squashed after upstream changed the\n   same files. It replaces --squashed\n * New prep commit lets delete_branches() take a map of where each branch\n   landed, so \"landed as\" is printed there\n * Added Junio's test that --delete-merged keeps a branch when an unrelated\n   upstream commit adds an identical file\n * --dry-run works with either option, and the two options can't be combined\n\nChanges in v3:\n\n * Split into multiple commits, one for rebase merges and one for squash\n   merges.\n * By default a branch counts as merged when an upstream commit has its\n   exact version of every file it changed. This never reads file contents,\n   so it stays cheap in a partial clone. It finds rebase merges, and squash\n   merges where upstream left those files alone.\n * Replace --no-squashed with an opt-in --squashed, which also finds\n   branches merged after upstream changed the same files by merging those\n   files.\n * Add a test for a rebase merge, and check that a squash onto changed files\n   is kept without --squashed and deleted with it.\n\nChanges in v2:\n\n * Diff each upstream commit once and share the result between all branches,\n   faster than v1.\n * Fix a branch merged with a merge commit being reported as \"landed as\" the\n   merge when another branch was searched first, and add a test for it. Show\n   the topic commit itself in \"landed as\" for work that arrived through a\n   merge.\n * Use git merge --squash as the example in the commit message and mention\n   GitHub's squash and rebase buttons.\n * Add --no-squashed to skip the squash search.\n\nHarald Nordgren (4):\n  branch: move the deleteMerged opt-out check into a helper\n  branch: let delete_branches() name where a branch landed\n  branch: add --delete-integrated to delete rebase and squash merged\n    branches\n  branch: let --delete-integrated find branches merged onto changed\n    files\n\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  23 +-\n builtin/branch.c                 | 363 +++++++++++++++++++++++++++++--\n t/t3200-branch.sh                | 315 +++++++++++++++++++++++++++\n 4 files changed, 679 insertions(+), 30 deletions(-)\n\n\nbase-commit: f7de81456940dd6f3274e461149bba12965de609\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2425%2FHaraldNordgren%2Fbranch-delete-squashed-v9\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2425/HaraldNordgren/branch-delete-squashed-v9\nPull-Request: https://github.com/git/git/pull/2425\n\nRange-diff vs v8:\n\n 1:  25d2441ce7 = 1:  37ef7636e0 branch: move the deleteMerged opt-out check into a helper\n 2:  18c3edb6e3 = 2:  c4513f9ec1 branch: let delete_branches() name where a branch landed\n 3:  7bc634c794 = 3:  1f922a0034 branch: add --delete-integrated to delete rebase and squash merged branches\n 4:  22658105ed ! 4:  ef65700303 branch: let --delete-integrated find branches merged onto changed files\n     @@ builtin/branch.c: static int branch_pushes_to_upstream(struct branch *branch,\n       struct branch_change {\n       \tchar *path;\n      -\tstruct object_id branch_oid;\n     +-\tunsigned short branch_mode;\n      +\tstruct object_id base_oid, branch_oid, upstream_oid;\n     - \tunsigned short branch_mode;\n     ++\tunsigned short base_mode, branch_mode;\n      +\tenum change_state state;\n       };\n       \n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n      +\t\t       &p->one->oid : null_oid(the_hash_algo));\n       \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n       \t\t       &p->two->oid : null_oid(the_hash_algo));\n     ++\t\tchange->base_mode = p->one->mode;\n       \t\tchange->branch_mode = p->two->mode;\n     -@@ builtin/branch.c: static void collect_branch_changes(struct landing_search *search,\n     + \t}\n       \tdiff_flush(&diffopt);\n       }\n       \n     @@ builtin/branch.c: static void collect_branch_changes(struct landing_search *sear\n      +\tif (is_null_oid(&change->base_oid) ||\n      +\t    is_null_oid(&change->branch_oid) ||\n      +\t    oideq(&change->upstream_oid, &change->base_oid) ||\n     -+\t    mode != change->branch_mode || !S_ISREG(mode))\n     ++\t    mode != change->branch_mode || !S_ISREG(mode) ||\n     ++\t    !S_ISREG(change->base_mode))\n      +\t\treturn CHANGE_MISSING;\n      +\treturn CHANGE_NEEDS_MERGE;\n       }\n     @@ t/t3200-branch.sh: test_expect_success '--delete-integrated deletes a squash mer\n      +\t\tEOF\n      +\t)\n      +'\n     ++\n     ++test_expect_success '--delete-integrated keeps a branch that replaced a submodule with a file' '\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\tgit init lib &&\n     ++\t\ttest_commit -C lib in-lib &&\n     ++\t\tgit add lib &&\n     ++\t\tgit commit -m \"add lib as a submodule\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\tsetup_repo_for_delete_merged &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit checkout -b replaces-lib --track origin/next &&\n     ++\t\trmdir lib &&\n     ++\t\techo \"from the branch\" >lib &&\n     ++\t\tgit add lib &&\n     ++\t\tgit commit -m \"replace lib with a file\" &&\n     ++\t\tgit push origin replaces-lib &&\n     ++\t\tgit checkout --detach\n     ++\t) &&\n     ++\t(\n     ++\t\tcd upstream &&\n     ++\t\tgit checkout next &&\n     ++\t\trm -rf lib &&\n     ++\t\techo \"from upstream\" >lib &&\n     ++\t\tgit add lib &&\n     ++\t\tgit commit -m \"replace lib with another file\" &&\n     ++\t\tgit checkout main\n     ++\t) &&\n     ++\t(\n     ++\t\tcd repo &&\n     ++\t\tgit fetch origin &&\n     ++\n     ++\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n     ++\t\ttest_must_be_empty actual &&\n     ++\n     ++\t\tcheck_branches <<-\\EOF\n     ++\t\tmain\n     ++\t\treplaces-lib\n     ++\t\tEOF\n     ++\t)\n     ++'\n      +\n       test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n       \tsetup_repo_for_delete_merged &&\n\n-- \ngitgitgadget\n\n"},{"id":"558940","messageId":"37ef7636e069ce528728c43cd79f678293e0d035.1795184664.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","subject":"[PATCH v9 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-20T14:24:21Z","receivedAt":"2026-11-20T14:24: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\nA later commit adds \"git branch --delete-integrated\", which deletes\nbranches whose tips are not reachable from their upstream. Those must\nhonor branch.<name>.deleteMerged=false as well. Move the check into a\nhelper so it can be used in both places.\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 32 +++++++++++++++++++-------------\n 1 file changed, 19 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b536b619bb..2c1a8b8395 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n+\t\t\t     unsigned int flags)\n+{\n+\tint opt_out;\n+\n+\tstrbuf_reset(key);\n+\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n+\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n+\t    opt_out)\n+\t\treturn false;\n+\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\tfprintf(stderr,\n+\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n+\t\t\tbranch_name, branch_name);\n+\treturn true;\n+}\n+\n static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t const char **argv, unsigned int flags)\n {\n@@ -852,7 +869,6 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\tconst char *branch_name;\n \t\tstruct branch *branch;\n \t\tconst char *upstream_refname;\n-\t\tint opt_out;\n \n \t\tif (!skip_prefix(branch_refname, \"refs/heads/\", &branch_name))\n \t\t\tBUG(\"filter returned non-branch ref '%s'\", branch_refname);\n@@ -870,18 +886,8 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n \t\t\tcontinue;\n \n-\t\tstrbuf_reset(&key);\n-\t\tstrbuf_addf(&key, \"branch.%s.deletemerged\", branch_name);\n-\t\tif (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&\n-\t\t    !opt_out) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n-\t\t\t\tfprintf(stderr,\n-\t\t\t\t\t_(\"Skipping '%s' (branch.%s.deleteMerged is false)\\n\"),\n-\t\t\t\t\tbranch_name, branch_name);\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t\tif (!branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n-- \ngitgitgadget\n\n\n"},{"id":"558941","messageId":"c4513f9ec1315c34b7a75b95bf50bba59043c274.1795184664.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","subject":"[PATCH v9 2/4] branch: let delete_branches() name where a branch landed","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-20T14:24:22Z","receivedAt":"2026-11-20T14:24:22Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA later commit lets \"git branch\" delete branches whose commits are not\nin their upstream but whose changes are, such as after a squash merge.\nTheir tips are not reachable from the upstream, so the merge check in\ndelete_branches() would refuse to delete them, and the output should\nsay where their changes went.\n\nLet delete_branches() take a map from branch names to the upstream\ncommit where each branch landed. A branch in the map skips the merge\ncheck and is reported as\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nNo caller passes a map yet, so there is no change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c | 30 +++++++++++++++++++++++++-----\n 1 file changed, 25 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 2c1a8b8395..6e6b04b11d 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -236,7 +236,7 @@ static void delete_branch_config(const char *branchname)\n }\n \n static int delete_branches(int argc, const char **argv, int kinds,\n-\t\t\t   unsigned int flags)\n+\t\t\t   unsigned int flags, struct strmap *landed_commits)\n {\n \tstruct commit *head_rev = NULL;\n \tstruct object_id oid;\n@@ -334,6 +334,8 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \t\t}\n \n \t\tif (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&\n+\t\t    !(landed_commits &&\n+\t\t      strmap_contains(landed_commits, bname.buf)) &&\n \t\t    check_branch_commit(bname.buf, name, &oid, head_rev, kinds,\n \t\t\t\t\tflags)) {\n \t\t\tif (!(flags & DELETE_BRANCH_SKIP_UNMERGED))\n@@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n \tfor_each_string_list_item(item, &refs_to_delete) {\n \t\tchar *describe_ref = item->util;\n \t\tchar *name = item->string;\n+\t\tstruct commit *landed = landed_commits ?\n+\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n+\t\tconst char *landed_abbrev = landed ?\n+\t\t\trepo_find_unique_abbrev(the_repository,\n+\t\t\t\t\t\t&landed->object.oid,\n+\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n+\n \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       name + branch_name_pos, describe_ref,\n+\t\t\t\t       landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n \t\t\t\t\tname + branch_name_pos, describe_ref);\n \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n \t\t\tchar *refname = name + branch_name_pos;\n-\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n+\t\t\tif (flags & DELETE_BRANCH_QUIET)\n+\t\t\t\t;\n+\t\t\telse if (landed)\n+\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n+\t\t\t\t       refname, describe_ref, landed_abbrev);\n+\t\t\telse\n \t\t\t\tprintf(remote_branch\n \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n@@ -901,7 +921,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags);\n+\t\t\t\t      flags, NULL);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -1141,7 +1161,7 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch name required\"));\n \t\tret = delete_branches(argc, argv, filter.kind,\n \t\t\t\t      (delete > 1 ? DELETE_BRANCH_FORCE : 0) |\n-\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0));\n+\t\t\t\t      (quiet ? DELETE_BRANCH_QUIET : 0), NULL);\n \t\tgoto out;\n \t} else if (delete_merged.nr) {\n \t\tret = delete_merged_branches(&delete_merged, argv,\n-- \ngitgitgadget\n\n\n"},{"id":"558942","messageId":"1f922a00344f6b86dcc0054b3117f1bbad247810.1795184664.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","subject":"[PATCH v9 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-20T14:24:23Z","receivedAt":"2026-11-20T14:24:23Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git rebase main topic\n    git checkout main\n    git merge --ff-only topic\n\nor with\n\n    git merge --squash topic\n    git commit\n\nis never deleted by \"git branch --delete-merged\". This is what the\n\"Rebase and merge\" and \"Squash and merge\" buttons on GitHub do. The\nupstream holds rewritten copies of the branch's work, so its tip is not\nreachable from it and it looks unmerged forever.\n\nAdd --delete-integrated, which does what --delete-merged does and also\ndeletes a branch when some upstream commit since the fork point has the\nbranch's version of every file the branch changed. That commit is named\nin the output so the user can see where the work went:\n\n    Deleted branch topic (was 1a2b3c4, landed as 9f8e7d6).\n\nThe first such commit is used, so the branch is deleted even if\nupstream later reverted or reworked the changes. Nothing is lost, since\nthat commit keeps them in the upstream history. Who made that commit\ndoes not matter, since the local branch is no longer needed either way.\nA branch whose changes only partly landed is kept.\n\n--delete-merged keeps deleting only branches whose own commits are in\nthe upstream, so it never removes a branch because somebody else made\nthe same change.\n\nThe check never reads file contents, so it stays cheap in a partial\nclone.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/branch.adoc |   8 +-\n Documentation/git-branch.adoc    |  19 ++-\n builtin/branch.c                 | 253 ++++++++++++++++++++++++++++++-\n t/t3200-branch.sh                | 176 +++++++++++++++++++++\n 4 files changed, 441 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\nindex c2acd89c99..be9b296bda 100644\n--- a/Documentation/config/branch.adoc\n+++ b/Documentation/config/branch.adoc\n@@ -106,7 +106,7 @@ for details).\n \n `branch.<name>.deleteMerged`::\n \tIf set to `false`, branch _<name>_ is exempt from\n-\t`git branch --delete-merged`.  Useful for a topic branch you\n-\tintend to develop further after an initial round has been\n-\tmerged upstream.  Defaults to true.  Explicit deletion via\n-\t`git branch -d` is unaffected.\n+\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n+\tfor a topic branch you intend to develop further after an\n+\tinitial round has been merged upstream.  Defaults to true.\n+\tExplicit deletion via `git branch -d` is unaffected.\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex a803c24858..b6d9cd01a4 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -26,6 +26,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>\n git branch (-d|-D) [-r] <branch-name>...\n git branch --edit-description [<branch-name>]\n git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]\n+git branch [--dry-run] (--delete-integrated <pattern>)... [<branch-pattern>...]\n \n DESCRIPTION\n -----------\n@@ -235,11 +236,21 @@ A branch whose work has not yet been merged into its upstream is\n silently skipped. Delete it with `git branch -D` if you want to\n remove it anyway.\n \n+`--delete-integrated <pattern>`::\n+\tLike `--delete-merged`, but also delete a branch whose changes\n+\tthe upstream already has even though its commits are not there,\n+\tas happens after a squash or rebase merge. Such a branch is\n+\tdeleted when some upstream commit since the fork point has its\n+\tversion of every file it changed, even if those changes were\n+\tlater reverted, and the message names the first such commit.\n+\tIt does not matter who made that commit. The same rules for\n+\twhich branches are kept apply as for `--delete-merged`.\n+\n `--dry-run`::\n-\tWith `--delete-merged`, print which branches would be\n-\tdeleted and exit without touching any ref.  Useful for\n-\tsanity-checking a wide pattern like `'origin/*'` before\n-\tcommitting to the deletion.\n+\tWith `--delete-merged` or `--delete-integrated`, print which\n+\tbranches would be deleted and exit without touching any ref.\n+\tUseful for sanity-checking a wide pattern like `'origin/*'`\n+\tbefore committing to the deletion.\n \n `-v`::\n `-vv`::\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 6e6b04b11d..ee92e71f51 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -29,6 +29,12 @@\n #include \"help.h\"\n #include \"advice.h\"\n #include \"commit-reach.h\"\n+#include \"commit-slab.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n+#include \"hex.h\"\n+#include \"revision.h\"\n+#include \"tree-walk.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -41,6 +47,8 @@ static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--format]\"),\n \tN_(\"git branch [<options>] (--delete-merged <pattern>)... \"\n \t   \"[<branch-pattern>...]\"),\n+\tN_(\"git branch [<options>] (--delete-integrated <pattern>)... \"\n+\t   \"[<branch-pattern>...]\"),\n \tNULL\n };\n \n@@ -201,6 +209,7 @@ enum delete_branch_flags {\n \tDELETE_BRANCH_SKIP_UNMERGED = (1 << 2),\n \tDELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),\n \tDELETE_BRANCH_DRY_RUN = (1 << 4),\n+\tDELETE_BRANCH_INTEGRATED = (1 << 5),\n };\n \n static int check_branch_commit(const char *branchname, const char *refname,\n@@ -844,6 +853,185 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+struct branch_change {\n+\tchar *path;\n+\tstruct object_id branch_oid;\n+\tunsigned short branch_mode;\n+};\n+\n+struct landing_search {\n+\tconst char *branch_name;\n+\tstruct commit *rev;\n+\tstruct commit *upstream;\n+\tstruct branch_change *changes;\n+\tsize_t changes_nr, changes_alloc;\n+};\n+\n+define_commit_slab(changed_paths, struct string_list *);\n+\n+static void collect_branch_changes(struct landing_search *search,\n+\t\t\t\t   struct commit *base)\n+{\n+\tstruct diff_options diffopt;\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiff_setup_done(&diffopt);\n+\tdiff_tree_oid(get_commit_tree_oid(base),\n+\t\t      get_commit_tree_oid(search->rev), \"\", &diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct branch_change *change;\n+\n+\t\tALLOC_GROW(search->changes, search->changes_nr + 1,\n+\t\t\t   search->changes_alloc);\n+\t\tchange = &search->changes[search->changes_nr++];\n+\t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n+\t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->branch_mode = p->two->mode;\n+\t}\n+\tdiff_flush(&diffopt);\n+}\n+\n+static int change_landed(const struct branch_change *change,\n+\t\t\t struct commit *commit)\n+{\n+\tstruct object_id oid;\n+\tunsigned short mode;\n+\n+\tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n+\t\t\t   change->path, &oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid);\n+\treturn oideq(&oid, &change->branch_oid) &&\n+\t       mode == change->branch_mode;\n+}\n+\n+static int changes_landed(struct landing_search *search,\n+\t\t\t  struct commit *commit)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (!change_landed(&search->changes[i], commit))\n+\t\t\treturn 0;\n+\treturn 1;\n+}\n+\n+static struct string_list *changed_paths_of(struct changed_paths *cache,\n+\t\t\t\t\t    struct diff_options *diffopt,\n+\t\t\t\t\t    struct commit *commit)\n+{\n+\tstruct string_list **paths = changed_paths_at(cache, commit);\n+\tstruct commit *parent;\n+\n+\tif (*paths)\n+\t\treturn *paths;\n+\n+\tCALLOC_ARRAY(*paths, 1);\n+\tstring_list_init_dup(*paths);\n+\tparent = commit->parents ? commit->parents->item : NULL;\n+\tif (parent && repo_parse_commit(the_repository, parent))\n+\t\tdie(_(\"could not parse commit %s\"),\n+\t\t    oid_to_hex(&parent->object.oid));\n+\tdiff_tree_oid(parent ? get_commit_tree_oid(parent) : NULL,\n+\t\t      get_commit_tree_oid(commit), \"\", diffopt);\n+\tfor (int i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\n+\t\tstring_list_append(*paths, p->two->path);\n+\t}\n+\tdiff_flush(diffopt);\n+\tstring_list_sort(*paths);\n+\treturn *paths;\n+}\n+\n+static void free_changed_paths(struct string_list **paths)\n+{\n+\tif (!*paths)\n+\t\treturn;\n+\tstring_list_clear(*paths, 0);\n+\tfree(*paths);\n+}\n+\n+static int touches_changes(struct string_list *paths,\n+\t\t\t   const struct landing_search *search)\n+{\n+\tfor (size_t i = 0; i < search->changes_nr; i++)\n+\t\tif (string_list_has_string(paths, search->changes[i].path))\n+\t\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static struct commit *find_landed_commit(struct landing_search *search,\n+\t\t\t\t\t struct changed_paths *cache,\n+\t\t\t\t\t struct diff_options *diffopt)\n+{\n+\tstruct commit *commit, *landed = NULL;\n+\tstruct strvec args = STRVEC_INIT;\n+\tstruct rev_info revs;\n+\n+\tstrvec_pushl(&args, \"rev-list\", \"--reverse\",\n+\t\t     oid_to_hex(&search->upstream->object.oid), NULL);\n+\tstrvec_pushf(&args, \"^%s\", oid_to_hex(&search->rev->object.oid));\n+\n+\trepo_init_revisions(the_repository, &revs, NULL);\n+\tsetup_revisions_from_strvec(&args, &revs, NULL);\n+\tif (prepare_revision_walk(&revs))\n+\t\tdie(_(\"revision walk setup failed\"));\n+\twhile (!landed && (commit = get_revision(&revs)))\n+\t\tif (touches_changes(changed_paths_of(cache, diffopt, commit),\n+\t\t\t\t    search) &&\n+\t\t    changes_landed(search, commit))\n+\t\t\tlanded = commit;\n+\trelease_revisions(&revs);\n+\tclear_commit_marks(search->upstream, ALL_REV_FLAGS);\n+\tclear_commit_marks(search->rev, ALL_REV_FLAGS);\n+\tstrvec_clear(&args);\n+\treturn landed;\n+}\n+\n+static void find_landed_commits(struct landing_search *searches,\n+\t\t\t\tsize_t searches_nr,\n+\t\t\t\tstruct strmap *landed_commits)\n+{\n+\tstruct changed_paths cache;\n+\tstruct diff_options diffopt;\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct landing_search *search = &searches[i];\n+\t\tstruct commit_list *merge_bases = NULL;\n+\n+\t\tif (repo_get_merge_bases(the_repository, search->upstream,\n+\t\t\t\t\t search->rev, &merge_bases) < 0)\n+\t\t\texit(128);\n+\t\tif (merge_bases)\n+\t\t\tcollect_branch_changes(search, merge_bases->item);\n+\t\tcommit_list_free(merge_bases);\n+\t}\n+\n+\trepo_diff_setup(the_repository, &diffopt);\n+\tdiffopt.flags.recursive = 1;\n+\tdiffopt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tdiffopt.no_free = 1;\n+\tdiff_setup_done(&diffopt);\n+\tinit_changed_paths(&cache);\n+\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tstruct commit *landed;\n+\n+\t\tif (!searches[i].changes_nr)\n+\t\t\tcontinue;\n+\t\tlanded = find_landed_commit(&searches[i], &cache, &diffopt);\n+\t\tif (landed)\n+\t\t\tstrmap_put(landed_commits, searches[i].branch_name,\n+\t\t\t\t   landed);\n+\t}\n+\n+\tdeep_clear_changed_paths(&cache, free_changed_paths);\n+\tdiffopt.no_free = 0;\n+\tdiff_free(&diffopt);\n+}\n+\n static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n \t\t\t     unsigned int flags)\n {\n@@ -869,6 +1057,9 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \tstruct ref_array candidates = { 0 };\n \tstruct strset deletable_branch_names = STRSET_INIT;\n \tstruct strset protected_branch_names = STRSET_INIT;\n+\tstruct strmap landed_commits = STRMAP_INIT;\n+\tstruct landing_search *searches = NULL;\n+\tsize_t searches_nr = 0, searches_alloc = 0;\n \tstruct strvec branches_to_delete = STRVEC_INIT;\n \tstruct strbuf key = STRBUF_INIT;\n \tstruct hashmap_iter iter;\n@@ -903,13 +1094,39 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\tcontinue;\n \t\tif (check_branch_commit(branch_name, branch_name,\n \t\t\t\t\t&candidates.items[i]->objectname, NULL,\n-\t\t\t\t\tFILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))\n+\t\t\t\t\tFILTER_REFS_BRANCHES,\n+\t\t\t\t\tDELETE_BRANCH_SKIP_UNMERGED)) {\n+\t\t\tstruct commit *rev, *upstream;\n+\n+\t\t\tif (!(flags & DELETE_BRANCH_INTEGRATED))\n+\t\t\t\tcontinue;\n+\t\t\trev = lookup_commit_reference(\n+\t\t\t\tthe_repository, &candidates.items[i]->objectname);\n+\t\t\tupstream = lookup_commit_reference_by_name(upstream_refname);\n+\t\t\tif (!rev || !upstream)\n+\t\t\t\tcontinue;\n+\t\t\tALLOC_GROW(searches, searches_nr + 1, searches_alloc);\n+\t\t\tsearches[searches_nr++] = (struct landing_search) {\n+\t\t\t\t.branch_name = branch_name,\n+\t\t\t\t.rev = rev,\n+\t\t\t\t.upstream = upstream,\n+\t\t\t};\n \t\t\tcontinue;\n+\t\t}\n \n \t\tif (!branch_opted_out(&key, branch_name, flags))\n \t\t\tstrset_add(&deletable_branch_names, branch_name);\n \t}\n \n+\tfind_landed_commits(searches, searches_nr, &landed_commits);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tconst char *branch_name = searches[i].branch_name;\n+\n+\t\tif (strmap_contains(&landed_commits, branch_name) &&\n+\t\t    !branch_opted_out(&key, branch_name, flags))\n+\t\t\tstrset_add(&deletable_branch_names, branch_name);\n+\t}\n+\n \tprotect_stacked_branch_bases(refs, &deletable_branch_names,\n \t\t\t\t     &protected_branch_names);\n \n@@ -921,7 +1138,7 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \t\t\t\t      FILTER_REFS_BRANCHES,\n \t\t\t\t      DELETE_BRANCH_SKIP_UNMERGED |\n \t\t\t\t      DELETE_BRANCH_NO_HEAD_FALLBACK |\n-\t\t\t\t      flags, NULL);\n+\t\t\t\t      flags, &landed_commits);\n \n \tif (!ret && !(flags & DELETE_BRANCH_DRY_RUN))\n \t\tclear_deleted_upstreams(&protected_branch_names,\n@@ -929,6 +1146,13 @@ static int delete_merged_branches(const struct strvec *upstreams,\n \n \tstrbuf_release(&key);\n \tstrvec_clear(&branches_to_delete);\n+\tfor (size_t i = 0; i < searches_nr; i++) {\n+\t\tfor (size_t j = 0; j < searches[i].changes_nr; j++)\n+\t\t\tfree(searches[i].changes[j].path);\n+\t\tfree(searches[i].changes);\n+\t}\n+\tfree(searches);\n+\tstrmap_clear(&landed_commits, 0);\n \tstrset_clear(&protected_branch_names);\n \tstrset_clear(&deletable_branch_names);\n \tref_array_clear(&candidates);\n@@ -1001,6 +1225,7 @@ int cmd_branch(int argc,\n \tint delete = 0, rename = 0, copy = 0, list = 0,\n \t    unset_upstream = 0, show_current = 0, edit_description = 0;\n \tstruct strvec delete_merged = STRVEC_INIT;\n+\tstruct strvec delete_integrated = STRVEC_INIT;\n \tint dry_run = 0;\n \tconst char *new_upstream = NULL;\n \tint noncreate_actions = 0;\n@@ -1058,8 +1283,11 @@ int cmd_branch(int argc,\n \t\tOPT_CALLBACK_F(0, \"delete-merged\", &delete_merged, N_(\"pattern\"),\n \t\t\tN_(\"delete merged branches whose upstream matches <pattern> (repeatable)\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n+\t\tOPT_CALLBACK_F(0, \"delete-integrated\", &delete_integrated, N_(\"pattern\"),\n+\t\t\tN_(\"like --delete-merged, but also delete branches whose changes landed upstream (repeatable)\"),\n+\t\t\tPARSE_OPT_NONEG, parse_opt_strvec),\n \t\tOPT_BOOL(0, \"dry-run\", &dry_run,\n-\t\t\tN_(\"with --delete-merged, only print which branches would be deleted\")),\n+\t\t\tN_(\"with --delete-merged or --delete-integrated, only print which branches would be deleted\")),\n \t\tOPT__FORCE(&force, N_(\"force creation, move/rename, deletion\"), PARSE_OPT_NOCOMPLETE),\n \t\tOPT_MERGED(&filter, N_(\"print only branches that are merged\")),\n \t\tOPT_NO_MERGED(&filter, N_(\"print only branches that are not merged\")),\n@@ -1108,7 +1336,7 @@ int cmd_branch(int argc,\n \n \tif (!delete && !rename && !copy && !edit_description && !new_upstream &&\n \t    !show_current && !unset_upstream && !delete_merged.nr &&\n-\t    argc == 0)\n+\t    !delete_integrated.nr && argc == 0)\n \t\tlist = 1;\n \n \tif (filter.with_commit || filter.no_commit ||\n@@ -1116,14 +1344,18 @@ int cmd_branch(int argc,\n \t    filter.points_at.nr || filter.forked.nr)\n \t\tlist = 1;\n \n+\tdie_for_incompatible_opt2(!!delete_merged.nr, \"--delete-merged\",\n+\t\t\t\t  !!delete_integrated.nr, \"--delete-integrated\");\n+\n \tnoncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +\n \t\t\t    !!show_current + !!list + !!edit_description +\n-\t\t\t    !!unset_upstream + !!delete_merged.nr;\n+\t\t\t    !!unset_upstream + !!delete_merged.nr +\n+\t\t\t    !!delete_integrated.nr;\n \tif (noncreate_actions > 1)\n \t\tusage_with_options(builtin_branch_usage, options);\n \n-\tif (dry_run && !delete_merged.nr)\n-\t\tdie(_(\"--dry-run requires --delete-merged\"));\n+\tif (dry_run && !delete_merged.nr && !delete_integrated.nr)\n+\t\tdie(_(\"--dry-run requires --delete-merged or --delete-integrated\"));\n \n \tif (recurse_submodules_explicit) {\n \t\tif (!submodule_propagate_branches)\n@@ -1168,6 +1400,12 @@ int cmd_branch(int argc,\n \t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n \t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0));\n \t\tgoto out;\n+\t} else if (delete_integrated.nr) {\n+\t\tret = delete_merged_branches(&delete_integrated, argv,\n+\t\t\t\t\t     (quiet ? DELETE_BRANCH_QUIET : 0) |\n+\t\t\t\t\t     (dry_run ? DELETE_BRANCH_DRY_RUN : 0) |\n+\t\t\t\t\t     DELETE_BRANCH_INTEGRATED);\n+\t\tgoto out;\n \t} else if (show_current) {\n \t\tprint_current_branch_name();\n \t\tret = 0;\n@@ -1347,6 +1585,7 @@ int cmd_branch(int argc,\n \n out:\n \tstrvec_clear(&delete_merged);\n+\tstrvec_clear(&delete_integrated);\n \tstring_list_clear(&sorting_options, 0);\n \treturn ret;\n }\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 44336eef81..54461e90b3 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1963,6 +1963,39 @@ check_branches () {\n \ttest_cmp expect actual\n }\n \n+test_expect_success '--delete-merged does not delete unrelated branches' '\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unrelated-topic --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"add file\" &&\n+\t\tgit push origin unrelated-topic &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 >file &&\n+\t\ttest_write_lines 5 6 7 >elif &&\n+\t\tgit add file elif &&\n+\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n+\t\t\t-m \"unrelated commit adding an identical file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tunrelated-topic\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n \tsetup_repo_for_delete_merged &&\n \t(\n@@ -2017,6 +2050,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n \t)\n '\n \n+push_topic () {\n+\tbranch=$1 &&\n+\tshift &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b \"$branch\" --track origin/next &&\n+\t\tfor commit in \"$@\"\n+\t\tdo\n+\t\t\ttest_commit \"$commit\" || return 1\n+\t\tdone &&\n+\t\tgit push origin \"$branch\" &&\n+\t\tgit checkout --detach\n+\t)\n+}\n+\n+squash_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit merge --squash \"$1\" &&\n+\t\tgit commit -m \"Squash merge of $1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+rebase_merge_upstream () {\n+\t(\n+\t\tcd upstream &&\n+\t\tgit rebase next \"$1\" &&\n+\t\tgit checkout next &&\n+\t\tgit merge --ff-only \"$1\" &&\n+\t\tgit checkout main\n+\t)\n+}\n+\n+test_expect_success '--delete-integrated deletes a rebase merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic rebased rebased-one rebased-two &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit before-rebase &&\n+\t\tgit checkout main\n+\t) &&\n+\trebase_merge_upstream rebased &&\n+\tlanded=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short rebased) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic squashed squashed-one squashed-two &&\n+\tpush_topic partial lands-upstream stays-local &&\n+\tsquash_merge_upstream partial~1 &&\n+\tsquash_merge_upstream squashed &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short squashed) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tpartial\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic reverted reverted-work &&\n+\tsquash_merge_upstream reverted &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit revert --no-edit HEAD &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short reverted) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n+\tsetup_repo_for_delete_merged &&\n+\tpush_topic a-unmerged unmerged-work &&\n+\tpush_topic b-merged merged-work &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_commit upstream-work &&\n+\t\tgit merge --no-ff b-merged &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short b-merged) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\ta-unmerged\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-merged keeps main despite a different default push remote' '\n \tsetup_repo_for_delete_merged &&\n \tcreate_merged_branch on-next &&\n@@ -2216,4 +2386,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n \ttest_grep \"requires --delete-merged\" err\n '\n \n+test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n+\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n+\t\t--delete-integrated origin/next 2>err &&\n+\ttest_grep \"cannot be used together\" err\n+'\n+\n test_done\n-- \ngitgitgadget\n\n\n"},{"id":"558943","messageId":"ef6570030325b4fe18624c30ad1779552fe46b37.1795184664.git.gitgitgadget@gmail.com","threadId":"66417","inReplyTo":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","subject":"[PATCH v9 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-20T14:24:24Z","receivedAt":"2026-11-20T14:24:24Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch that upstream integrated with\n\n    git merge --squash topic\n    git commit\n\nafter upstream had also changed the files the branch touches is still\nkept by \"git branch --delete-integrated\". The squash commit has neither\nthe branch's version of those files nor the fork point's, so the\nprevious check cannot see that the work landed. This happens all the\ntime on hosting services such as GitHub, where the \"Squash and merge\"\nbutton lands pull requests this way, and other pull requests often\ntouch the same files first. A rebase merge onto such changes looks the\nsame.\n\nFor each such file, merge the branch's version into the upstream one\nand treat the change as landed when that leaves the upstream version\nunchanged. This has to read the files, so in a partial clone it may\ndownload blobs.\n\nThe merge uses the attributes of this repository, from the working tree\nand the index, the same as a merge run here would. A merge driver that\nonly the upstream repository has is not seen. In a bare repository only\ninfo/attributes and the global file apply.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-branch.adoc |  14 ++--\n builtin/branch.c              |  76 ++++++++++++++++---\n t/t3200-branch.sh             | 139 ++++++++++++++++++++++++++++++++++\n 3 files changed, 213 insertions(+), 16 deletions(-)\n\ndiff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc\nindex b6d9cd01a4..814395d223 100644\n--- a/Documentation/git-branch.adoc\n+++ b/Documentation/git-branch.adoc\n@@ -240,11 +240,15 @@ remove it anyway.\n \tLike `--delete-merged`, but also delete a branch whose changes\n \tthe upstream already has even though its commits are not there,\n \tas happens after a squash or rebase merge. Such a branch is\n-\tdeleted when some upstream commit since the fork point has its\n-\tversion of every file it changed, even if those changes were\n-\tlater reverted, and the message names the first such commit.\n-\tIt does not matter who made that commit. The same rules for\n-\twhich branches are kept apply as for `--delete-merged`.\n+\tdeleted when some upstream commit since the fork point has all\n+\tof its changes, even if they were later reverted, and the\n+\tmessage names the first such commit. It does not matter who made\n+\tthat commit. A file counts when the commit has the branch's\n+\tversion of it. It also counts when merging the branch's version\n+\tinto the commit's version leaves the commit's version unchanged.\n+\tThat merge reads the file, and in a partial clone it may download\n+\tblobs. The same rules for which branches are kept apply as for\n+\t`--delete-merged`.\n \n `--dry-run`::\n \tWith `--delete-merged` or `--delete-integrated`, print which\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex ee92e71f51..262cd82272 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -33,8 +33,10 @@\n #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"hex.h\"\n+#include \"merge-ll.h\"\n #include \"revision.h\"\n #include \"tree-walk.h\"\n+#include \"xdiff-interface.h\"\n \n static const char * const builtin_branch_usage[] = {\n \tN_(\"git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]\"),\n@@ -853,10 +855,17 @@ static int branch_pushes_to_upstream(struct branch *branch,\n \treturn ret;\n }\n \n+enum change_state {\n+\tCHANGE_MISSING,\n+\tCHANGE_LANDED,\n+\tCHANGE_NEEDS_MERGE,\n+};\n+\n struct branch_change {\n \tchar *path;\n-\tstruct object_id branch_oid;\n-\tunsigned short branch_mode;\n+\tstruct object_id base_oid, branch_oid, upstream_oid;\n+\tunsigned short base_mode, branch_mode;\n+\tenum change_state state;\n };\n \n struct landing_search {\n@@ -888,32 +897,76 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t\t   search->changes_alloc);\n \t\tchange = &search->changes[search->changes_nr++];\n \t\tchange->path = xstrdup(p->two->path);\n+\t\toidcpy(&change->base_oid, DIFF_FILE_VALID(p->one) ?\n+\t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->base_mode = p->one->mode;\n \t\tchange->branch_mode = p->two->mode;\n \t}\n \tdiff_flush(&diffopt);\n }\n \n-static int change_landed(const struct branch_change *change,\n-\t\t\t struct commit *commit)\n+static int merge_keeps_upstream(const struct branch_change *change)\n+{\n+\tmmfile_t base, upstream, branch;\n+\tmmfile_t result = { 0 };\n+\tint ret;\n+\n+\tread_mmblob(&base, the_repository->objects, &change->base_oid);\n+\tread_mmblob(&upstream, the_repository->objects, &change->upstream_oid);\n+\tread_mmblob(&branch, the_repository->objects, &change->branch_oid);\n+\tret = ll_merge(&result, change->path, &base, \"base\",\n+\t\t       &upstream, \"upstream\", &branch, \"branch\",\n+\t\t       the_repository->index, NULL) == LL_MERGE_OK &&\n+\t      result.size == upstream.size &&\n+\t      !memcmp(result.ptr, upstream.ptr, upstream.size);\n+\n+\tfree(base.ptr);\n+\tfree(upstream.ptr);\n+\tfree(branch.ptr);\n+\tfree(result.ptr);\n+\treturn ret;\n+}\n+\n+static enum change_state change_state_at(struct branch_change *change,\n+\t\t\t\t\t struct commit *commit)\n {\n-\tstruct object_id oid;\n \tunsigned short mode;\n \n \tif (get_tree_entry(the_repository, get_commit_tree_oid(commit),\n-\t\t\t   change->path, &oid, &mode))\n-\t\treturn is_null_oid(&change->branch_oid);\n-\treturn oideq(&oid, &change->branch_oid) &&\n-\t       mode == change->branch_mode;\n+\t\t\t   change->path, &change->upstream_oid, &mode))\n+\t\treturn is_null_oid(&change->branch_oid) ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (oideq(&change->upstream_oid, &change->branch_oid))\n+\t\treturn mode == change->branch_mode ?\n+\t\t\tCHANGE_LANDED : CHANGE_MISSING;\n+\tif (is_null_oid(&change->base_oid) ||\n+\t    is_null_oid(&change->branch_oid) ||\n+\t    oideq(&change->upstream_oid, &change->base_oid) ||\n+\t    mode != change->branch_mode || !S_ISREG(mode) ||\n+\t    !S_ISREG(change->base_mode))\n+\t\treturn CHANGE_MISSING;\n+\treturn CHANGE_NEEDS_MERGE;\n }\n \n static int changes_landed(struct landing_search *search,\n \t\t\t  struct commit *commit)\n {\n-\tfor (size_t i = 0; i < search->changes_nr; i++)\n-\t\tif (!change_landed(&search->changes[i], commit))\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tchange->state = change_state_at(change, commit);\n+\t\tif (change->state == CHANGE_MISSING)\n \t\t\treturn 0;\n+\t}\n+\tfor (size_t i = 0; i < search->changes_nr; i++) {\n+\t\tstruct branch_change *change = &search->changes[i];\n+\n+\t\tif (change->state == CHANGE_NEEDS_MERGE &&\n+\t\t    !merge_keeps_upstream(change))\n+\t\t\treturn 0;\n+\t}\n \treturn 1;\n }\n \n@@ -1015,6 +1068,7 @@ static void find_landed_commits(struct landing_search *searches,\n \tdiffopt.no_free = 1;\n \tdiff_setup_done(&diffopt);\n \tinit_changed_paths(&cache);\n+\trepo_read_index(the_repository);\n \n \tfor (size_t i = 0; i < searches_nr; i++) {\n \t\tstruct commit *landed;\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 54461e90b3..ed62164a95 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -2160,6 +2160,145 @@ test_expect_success '--delete-integrated deletes a squash merged branch that was\n \t)\n '\n \n+test_expect_success '--delete-integrated deletes a squash merged branch after upstream changed the same file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit add shared &&\n+\t\tgit commit -m \"add shared\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b edits-shared --track origin/next &&\n+\t\ttest_write_lines 1 two 3 4 5 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 2\" &&\n+\t\tgit push origin edits-shared &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >shared &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsquash_merge_upstream edits-shared &&\n+\tsquash=$(git -C upstream rev-parse --short next) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\t\tsha=$(git rev-parse --short edits-shared) &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\techo \"Deleted branch edits-shared (was $sha, landed as $squash).\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated keeps branches whose changes did not land' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 8 9 10 >doc &&\n+\t\tgit add doc &&\n+\t\tgit commit -m \"add doc\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b unmerged --track origin/next &&\n+\t\ttest_write_lines 1 2 3 4 5 6 7 eight 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 8\" &&\n+\t\tgit push origin unmerged &&\n+\t\tgit checkout -b half-landed --track origin/next &&\n+\t\ttest_write_lines one 2 3 4 5 6 7 8 9 ten >doc &&\n+\t\tgit commit -a -m \"edit lines 1 and 10\" &&\n+\t\tgit push origin half-landed &&\n+\t\tgit checkout -b conflicting --track origin/next &&\n+\t\ttest_write_lines 1 2 three 4 5 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3\" &&\n+\t\tgit push origin conflicting &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\ttest_write_lines 1 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 5\" &&\n+\t\ttest_write_lines one 2 3 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"take line 1 from half-landed\" &&\n+\t\ttest_write_lines one 2 THREE 4 five 6 7 8 9 10 >doc &&\n+\t\tgit commit -a -m \"edit line 3 differently\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tconflicting\n+\t\thalf-landed\n+\t\tmain\n+\t\tunmerged\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--delete-integrated keeps a branch that replaced a submodule with a file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit init lib &&\n+\t\ttest_commit -C lib in-lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"add lib as a submodule\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b replaces-lib --track origin/next &&\n+\t\trmdir lib &&\n+\t\techo \"from the branch\" >lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"replace lib with a file\" &&\n+\t\tgit push origin replaces-lib &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\trm -rf lib &&\n+\t\techo \"from upstream\" >lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"replace lib with another file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\treplaces-lib\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n-- \ngitgitgadget\n\n"},{"id":"558987","messageId":"xmqqy0anpbzb.fsf@gitster.g","threadId":"66417","inReplyTo":"37ef7636e069ce528728c43cd79f678293e0d035.1795184664.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v9 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T18:57:28Z","receivedAt":"2026-11-20T18:57:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A later commit adds \"git branch --delete-integrated\", which deletes\n> branches whose tips are not reachable from their upstream. Those must\n> honor branch.<name>.deleteMerged=false as well. Move the check into a\n> helper so it can be used in both places.\n>\n> No change in behavior.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  builtin/branch.c | 32 +++++++++++++++++++-------------\n>  1 file changed, 19 insertions(+), 13 deletions(-)\n>\n> diff --git a/builtin/branch.c b/builtin/branch.c\n> index b536b619bb..2c1a8b8395 100644\n> --- a/builtin/branch.c\n> +++ b/builtin/branch.c\n> @@ -824,6 +824,23 @@ static int branch_pushes_to_upstream(struct branch *branch,\n>  \treturn ret;\n>  }\n>  \n> +static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n> +\t\t\t     unsigned int flags)\n\nI am afraid that this is a somewhat selfish choice of a name that we\nwould regret later, as it assumes that \"delete once it is merged\nelsewhere\" will continue to be the only destructive operation that\npeople may want to exempt some branches from.  The configuration key\nis named very specifically for that destructive operation (i.e.,\nsetting branch.<name>.deleteMerged to false is a way to say \"no,\neven if it is merged, do not delete it\"), so there is an impedance\nmismatch.\n\nEither\n\n (1) naming this function to make it clear that this is about\n     opting out of the deleteMerged operation, or\n\n (2) naming this function to make it clear that this is about opting\n     out of ANY destructive operation, and renaming the\n     configuration key to something less specific than deleteMerged\n     so that the user will be saved from any new destructive\n     operation we may add in the future, e.g., \"force update to what\n     upstream says\", by setting the same configuration variable to\n     opt out of it (e.g., branch.<name>.noClobber=true)\n\nwould work better, perhaps?\n\n> +{\n> +\tint opt_out;\n> +\n> +\tstrbuf_reset(key);\n> +\tstrbuf_addf(key, \"branch.%s.deletemerged\", branch_name);\n> +\tif (repo_config_get_bool(the_repository, key->buf, &opt_out) ||\n> +\t    opt_out)\n> +\t\treturn false;\n\nThe lack of a key, or the key explicitly set to true, will cause\nthis function to say \"no, this branch is not opted out\", which\nsmells like a risky default to me.\n\nPutting the devil's advocate hat on (because I understand why the\ninventor of a feature wants it to apply as widely as possible),\nshouldn't this be the other way around?  If the user does not\nexplicitly say \"I am fine losing this once we are done\", shouldn't\nwe be treating it as precious?\n\n"},{"id":"558991","messageId":"xmqqmrr3pbdo.fsf@gitster.g","threadId":"66417","inReplyTo":"xmqqy0anpbzb.fsf@gitster.g","subject":"Re: [PATCH v9 1/4] branch: move the deleteMerged opt-out check into a helper","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T19:10:27Z","receivedAt":"2026-11-20T19:10:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n>> +static bool branch_opted_out(struct strbuf *key, const char *branch_name,\n>> +\t\t\t     unsigned int flags)\n>\n> I am afraid that this is a somewhat selfish choice of a name that we\n> would regret later, as it assumes that \"delete once it is merged\n> elsewhere\" will continue to be the only destructive operation that\n> people may want to exempt some branches from.  The configuration key\n> is named very specifically for that destructive operation (i.e.,\n> setting branch.<name>.deleteMerged to false is a way to say \"no,\n> even if it is merged, do not delete it\"), so there is an impedance\n> mismatch.\n>\n> Either\n>\n>  (1) naming this function to make it clear that this is about\n>      opting out of the deleteMerged operation, or\n>\n>  (2) naming this function to make it clear that this is about opting\n>      out of ANY destructive operation, and renaming the\n>      configuration key to something less specific than deleteMerged\n>      so that the user will be saved from any new destructive\n>      operation we may add in the future, e.g., \"force update to what\n>      upstream says\", by setting the same configuration variable to\n>      opt out of it (e.g., branch.<name>.noClobber=true)\n>\n> would work better, perhaps?\n\nI forgot that there is a third option.  \n\nbranch_opted_out(\"main\", \"deleteMerged\") can be a boolean function\nthat asks if the user is OK with subjecting the \"main\" branch to the\n\"deleteMerged\" operation (and the implementation may be done by\nchecking the \"branch.<name>.deleteMerged\" configuration variable).\nThe function must continue to be purely about \"is the branch opted\nout of operation X\" without adding X-specific side effects, if we go\nthat route.\n\nThanks.\n\n"},{"id":"558993","messageId":"xmqqeccfpacc.fsf@gitster.g","threadId":"66417","inReplyTo":"c4513f9ec1315c34b7a75b95bf50bba59043c274.1795184664.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v9 2/4] branch: let delete_branches() name where a branch landed","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T19:32:51Z","receivedAt":"2026-11-20T19:32:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> @@ -357,15 +359,33 @@ static int delete_branches(int argc, const char **argv, int kinds,\n>  \tfor_each_string_list_item(item, &refs_to_delete) {\n>  \t\tchar *describe_ref = item->util;\n>  \t\tchar *name = item->string;\n> +\t\tstruct commit *landed = landed_commits ?\n> +\t\t\tstrmap_get(landed_commits, name + branch_name_pos) : NULL;\n> +\t\tconst char *landed_abbrev = landed ?\n> +\t\t\trepo_find_unique_abbrev(the_repository,\n> +\t\t\t\t\t\t&landed->object.oid,\n> +\t\t\t\t\t\tDEFAULT_ABBREV) : NULL;\n> +\n>  \t\tif (flags & DELETE_BRANCH_DRY_RUN) {\n> -\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n> +\t\t\tif (flags & DELETE_BRANCH_QUIET)\n> +\t\t\t\t;\n> +\t\t\telse if (landed)\n> +\t\t\t\tprintf(_(\"Would delete branch %s (was %s, landed as %s).\\n\"),\n> +\t\t\t\t       name + branch_name_pos, describe_ref,\n> +\t\t\t\t       landed_abbrev);\n> +\t\t\telse\n>  \t\t\t\tprintf(remote_branch\n>  \t\t\t\t\t? _(\"Would delete remote-tracking branch %s (was %s).\\n\")\n>  \t\t\t\t\t: _(\"Would delete branch %s (was %s).\\n\"),\n>  \t\t\t\t\tname + branch_name_pos, describe_ref);\n\nThe use of an empty \"if\" statement body is a clean way to express\nthat nothing will be emitted under \"--quiet\" without making the code\ntoo deeply nested.  Nicely done.\n\nThis is not a new issue, but this makes me wonder if giving the\n\"--dry-run\" and the \"--quiet\" options together makes sense.  Would\nusers really gain much information from \"--quiet\" when they are so\nparanoid as to want a preview of what would happen with \"--dry-run\"?\n\n>  \t\t} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {\n>  \t\t\tchar *refname = name + branch_name_pos;\n> -\t\t\tif (!(flags & DELETE_BRANCH_QUIET))\n> +\t\t\tif (flags & DELETE_BRANCH_QUIET)\n> +\t\t\t\t;\n> +\t\t\telse if (landed)\n> +\t\t\t\tprintf(_(\"Deleted branch %s (was %s, landed as %s).\\n\"),\n> +\t\t\t\t       refname, describe_ref, landed_abbrev);\n> +\t\t\telse\n>  \t\t\t\tprintf(remote_branch\n>  \t\t\t\t\t? _(\"Deleted remote-tracking branch %s (was %s).\\n\")\n>  \t\t\t\t\t: _(\"Deleted branch %s (was %s).\\n\"),\n\nLikewise.\n\nI wonder if it makes the code structure cleaner to follow if we swap\nthe order of checks between \"are we doing dry-run\" (which is on the\noutside in the current code structure) and \"what is the reason we\nremove this branch\" (which is the inner if/elseif cascade) by having\nthese different message templates (currently a pair of three-message\nsets) in an array and choosing which set to use based on\n\"--dry-run\".  There are of course a few things that are done only in\nthe non-dry-run mode (like removing the branch configuration and the\nbranch itself).  In other words, a code structure like this\n\n\tconst char *msg_templates[2][3] = {\n\t\t{ \"Would delete branch %s ...\", ..., ..., },\n\t\t{ \"Deleted branch %s ...\", ..., ..., },\n\t};\n\tconst char *use_msg[3] = msg_templates[!dry_run];\n\n\tfor-each-branch-to-be-deleted {\n\t\tcompute if branch has landed;\n\t\tif (quiet)\n\t\t\t;\n\t\telse if (landed)\n\t\t\tprintf(_(use_msg[0]), ...);\n\t\telse if (remote_branch)\n\t\t\tprintf(_(use_msg[1]), ...);\n\t\telse\n\t\t\tprintf(_(use_msg[2]), ...);\n\t\tif (!dry_run)\n\t\t\tremove branch and its configuration;\n\t\telse\n\t\t\twhat is only done in dry-run mode (if exists)\n\t}\n\t\t\nmay make it easier to follow for readers?\n\n"},{"id":"558996","messageId":"xmqqzev3nue9.fsf@gitster.g","threadId":"66417","inReplyTo":"1f922a00344f6b86dcc0054b3117f1bbad247810.1795184664.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v9 3/4] branch: add --delete-integrated to delete rebase and squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T20:02:38Z","receivedAt":"2026-11-20T20:02:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Subject: Re: [PATCH v9 3/4] branch: add --delete-integrated to delete rebase and squash merged branches\n\nThe title is on a tad overlong side.\n\n\"rebase and squash merged branches\" sounds awkward.  If a branch got\nintergrated by squash merging is called \"squash-merged branch\",\nshouldn't a branch that underwent \"git rebase\" to sit on top of the\nupstream be called \"rebased branch\"?\n\nIOW, \"rebased or squash-merged branches\"?\n\n> diff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc\n> index c2acd89c99..be9b296bda 100644\n> --- a/Documentation/config/branch.adoc\n> +++ b/Documentation/config/branch.adoc\n> @@ -106,7 +106,7 @@ for details).\n>  \n>  `branch.<name>.deleteMerged`::\n>  \tIf set to `false`, branch _<name>_ is exempt from\n> -\t`git branch --delete-merged`.  Useful for a topic branch you\n> -\tintend to develop further after an initial round has been\n> -\tmerged upstream.  Defaults to true.  Explicit deletion via\n> -\t`git branch -d` is unaffected.\n> +\t`git branch --delete-merged` and `--delete-integrated`.  Useful\n> +\tfor a topic branch you intend to develop further after an\n> +\tinitial round has been merged upstream.  Defaults to true.\n> +\tExplicit deletion via `git branch -d` is unaffected.\n\nDoes the deleteMerged configuration variable affect the behavior of\nthe \"--delete-integrated\" option?  Knowing how the feature evolved,\nit is understandable to me, but I suspect it won't be natural for\nmere mortals.\n\nPerhaps a single .deleteDone variable that governs both would be\nmore future-proof, as it anticipates that we may find other ways a\nbranch becomes \"done\", other than by getting merged or otherwise\nintegrated, and when that happens this variable will also deal with\nsuch a new destructive operation.\n\n> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\n> index 44336eef81..54461e90b3 100755\n> --- a/t/t3200-branch.sh\n> +++ b/t/t3200-branch.sh\n> @@ -1963,6 +1963,39 @@ check_branches () {\n>  \ttest_cmp expect actual\n>  }\n>  \n> +test_expect_success '--delete-merged does not delete unrelated branches' '\n> +\tsetup_repo_for_delete_merged &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit checkout -b unrelated-topic --track origin/next &&\n> +\t\ttest_write_lines 1 2 3 4 >file &&\n> +\t\tgit add file &&\n> +\t\tgit commit -m \"add file\" &&\n> +\t\tgit push origin unrelated-topic &&\n> +\t\tgit checkout --detach\n> +\t) &&\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> +\t\ttest_write_lines 1 2 3 4 >file &&\n> +\t\ttest_write_lines 5 6 7 >elif &&\n> +\t\tgit add file elif &&\n> +\t\tgit commit --author=\"unrelated user <uu@example.com>\" \\\n> +\t\t\t-m \"unrelated commit adding an identical file\" &&\n> +\t\tgit checkout main\n> +\t) &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit fetch origin &&\n> +\t\tgit branch --delete-merged origin/next >actual 2>&1 &&\n> +\t\ttest_must_be_empty actual &&\n> +\t\tcheck_branches <<-\\EOF\n> +\t\tmain\n> +\t\tunrelated-topic\n> +\t\tEOF\n> +\t)\n> +'\n> +\n>  test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '\n>  \tsetup_repo_for_delete_merged &&\n>  \t(\n> @@ -2017,6 +2050,143 @@ test_expect_success '--delete-merged deletes only selected merged branches' '\n>  \t)\n>  '\n>  \n> +push_topic () {\n> +\tbranch=$1 &&\n> +\tshift &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit checkout -b \"$branch\" --track origin/next &&\n> +\t\tfor commit in \"$@\"\n> +\t\tdo\n> +\t\t\ttest_commit \"$commit\" || return 1\n> +\t\tdone &&\n> +\t\tgit push origin \"$branch\" &&\n> +\t\tgit checkout --detach\n> +\t)\n> +}\n> +\n> +squash_merge_upstream () {\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> +\t\tgit merge --squash \"$1\" &&\n> +\t\tgit commit -m \"Squash merge of $1\" &&\n> +\t\tgit checkout main\n> +\t)\n> +}\n> +\n> +rebase_merge_upstream () {\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit rebase next \"$1\" &&\n> +\t\tgit checkout next &&\n> +\t\tgit merge --ff-only \"$1\" &&\n> +\t\tgit checkout main\n> +\t)\n> +}\n> +\n> +test_expect_success '--delete-integrated deletes a rebase merged branch' '\n> +\tsetup_repo_for_delete_merged &&\n> +\tpush_topic rebased rebased-one rebased-two &&\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> +\t\ttest_commit before-rebase &&\n> +\t\tgit checkout main\n> +\t) &&\n> +\trebase_merge_upstream rebased &&\n> +\tlanded=$(git -C upstream rev-parse --short next) &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit fetch origin &&\n> +\t\tsha=$(git rev-parse --short rebased) &&\n> +\n> +\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n> +\t\techo \"Deleted branch rebased (was $sha, landed as $landed).\" >expect &&\n> +\t\ttest_cmp expect actual &&\n> +\n> +\t\tcheck_branches <<-\\EOF\n> +\t\tmain\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success '--delete-integrated deletes a squash merged branch' '\n> +\tsetup_repo_for_delete_merged &&\n> +\tpush_topic squashed squashed-one squashed-two &&\n> +\tpush_topic partial lands-upstream stays-local &&\n> +\tsquash_merge_upstream partial~1 &&\n> +\tsquash_merge_upstream squashed &&\n> +\tsquash=$(git -C upstream rev-parse --short next) &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit fetch origin &&\n> +\t\tsha=$(git rev-parse --short squashed) &&\n> +\n> +\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n> +\t\techo \"Deleted branch squashed (was $sha, landed as $squash).\" >expect &&\n> +\t\ttest_cmp expect actual &&\n> +\n> +\t\tcheck_branches <<-\\EOF\n> +\t\tmain\n> +\t\tpartial\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success '--delete-integrated deletes a squash merged branch that was reverted' '\n> +\tsetup_repo_for_delete_merged &&\n> +\tpush_topic reverted reverted-work &&\n> +\tsquash_merge_upstream reverted &&\n> +\tsquash=$(git -C upstream rev-parse --short next) &&\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> +\t\tgit revert --no-edit HEAD &&\n> +\t\tgit checkout main\n> +\t) &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit fetch origin &&\n> +\t\tsha=$(git rev-parse --short reverted) &&\n> +\n> +\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n> +\t\techo \"Deleted branch reverted (was $sha, landed as $squash).\" >expect &&\n> +\t\ttest_cmp expect actual &&\n> +\n> +\t\tcheck_branches <<-\\EOF\n> +\t\tmain\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n> +\tsetup_repo_for_delete_merged &&\n> +\tpush_topic a-unmerged unmerged-work &&\n> +\tpush_topic b-merged merged-work &&\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> +\t\ttest_commit upstream-work &&\n> +\t\tgit merge --no-ff b-merged &&\n> +\t\tgit checkout main\n> +\t) &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\tgit fetch origin &&\n> +\t\tsha=$(git rev-parse --short b-merged) &&\n> +\n> +\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n> +\t\techo \"Deleted branch b-merged (was $sha).\" >expect &&\n> +\t\ttest_cmp expect actual &&\n> +\n> +\t\tcheck_branches <<-\\EOF\n> +\t\ta-unmerged\n> +\t\tmain\n> +\t\tEOF\n> +\t)\n> +'\n\nDoes \"--dry-run --delete-integrated\" combination have test coverage?\nIf not shouldn't it?\n\nDoes opting out of \"--delete-integrated\" feature by setting\nbranch.<name>.deleteMerged to false have test coverage?\nIf not shouldn't it?\n\n>  test_expect_success '--delete-merged keeps main despite a different default push remote' '\n>  \tsetup_repo_for_delete_merged &&\n>  \tcreate_merged_branch on-next &&\n> @@ -2216,4 +2386,10 @@ test_expect_success '--dry-run without --delete-merged is rejected' '\n>  \ttest_grep \"requires --delete-merged\" err\n>  '\n\nIt now requires either --delete-merged or --delete-integrated.\nAt least, updating the expected message in the test we can see would\nbe helpful.  \n\nHaving said that, I do not agree with the design to make \"--dry-run\"\nrequire anything.\n\nIf \"git branch delete master\" is run, and 'master' is checked out,\nit would error out.  \"git branch delete --dry-run master\" should say\n\"we would not let you delete the currently checked out branch\" the\nsame way and exit, no?\n\n\n> +test_expect_success '--delete-integrated cannot be combined with --delete-merged' '\n> +\ttest_must_fail git -C forked branch --delete-merged origin/next \\\n> +\t\t--delete-integrated origin/next 2>err &&\n> +\ttest_grep \"cannot be used together\" err\n> +'\n\nGood.  You added die-for-incompatible-opt to make them incompatible.\n\nAlso do we want to ensure \"--quiet\" does what it is designed to do?\nI do not think I saw anything added to t3200 by this series that\nmentioned the option.\n\nThanks.\n\n"},{"id":"558997","messageId":"xmqqv75rnu3v.fsf@gitster.g","threadId":"66417","inReplyTo":"ef6570030325b4fe18624c30ad1779552fe46b37.1795184664.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v9 4/4] branch: let --delete-integrated find branches merged onto changed files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T20:08:52Z","receivedAt":"2026-11-20T20:08:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> +test_expect_success '--delete-integrated keeps a branch that replaced a submodule with a file' '\n> +\t(\n> +\t\tcd upstream &&\n> +\t\tgit checkout next &&\n> ...\n> +\t\tEOF\n> +\t)\n> +'\n\nThis test looks somewhat familiar.\n\nWas this the only corner case that needed a fix?  From the code\nchange (below), it seems to be the case, and I was hoping that since\nreviewers are doing a lot of work finding corner cases, the original\nauthor would be finding three times as many corner cases to make the\ntopic better, instead of just reacting to a case that was reported\n;-)\n\n\ndiff --git c/builtin/branch.c w/builtin/branch.c\nindex 7b1048b583..a225c591b8 100644\n--- c/builtin/branch.c\n+++ w/builtin/branch.c\n@@ -864,7 +864,7 @@ enum change_state {\n struct branch_change {\n \tchar *path;\n \tstruct object_id base_oid, branch_oid, upstream_oid;\n-\tunsigned short branch_mode;\n+\tunsigned short base_mode, branch_mode;\n \tenum change_state state;\n };\n \n@@ -901,6 +901,7 @@ static void collect_branch_changes(struct landing_search *search,\n \t\t       &p->one->oid : null_oid(the_hash_algo));\n \t\toidcpy(&change->branch_oid, DIFF_FILE_VALID(p->two) ?\n \t\t       &p->two->oid : null_oid(the_hash_algo));\n+\t\tchange->base_mode = p->one->mode;\n \t\tchange->branch_mode = p->two->mode;\n \t}\n \tdiff_flush(&diffopt);\n@@ -943,7 +944,8 @@ static enum change_state change_state_at(struct branch_change *change,\n \tif (is_null_oid(&change->base_oid) ||\n \t    is_null_oid(&change->branch_oid) ||\n \t    oideq(&change->upstream_oid, &change->base_oid) ||\n-\t    mode != change->branch_mode || !S_ISREG(mode))\n+\t    mode != change->branch_mode || !S_ISREG(mode) ||\n+\t    !S_ISREG(change->base_mode))\n \t\treturn CHANGE_MISSING;\n \treturn CHANGE_NEEDS_MERGE;\n }\ndiff --git c/t/t3200-branch.sh w/t/t3200-branch.sh\nindex 78b7d395e2..41b31c8f67 100755\n--- c/t/t3200-branch.sh\n+++ w/t/t3200-branch.sh\n@@ -2217,6 +2217,50 @@ test_expect_success '--delete-integrated keeps branches whose changes did not la\n \t)\n '\n \n+test_expect_success '--delete-integrated keeps a branch that replaced a submodule with a file' '\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\tgit init lib &&\n+\t\ttest_commit -C lib in-lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"add lib as a submodule\" &&\n+\t\tgit checkout main\n+\t) &&\n+\tsetup_repo_for_delete_merged &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit checkout -b replaces-lib --track origin/next &&\n+\t\trmdir lib &&\n+\t\techo \"from the branch\" >lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"replace lib with a file\" &&\n+\t\tgit push origin replaces-lib &&\n+\t\tgit checkout --detach\n+\t) &&\n+\t(\n+\t\tcd upstream &&\n+\t\tgit checkout next &&\n+\t\trm -rf lib &&\n+\t\techo \"from upstream\" >lib &&\n+\t\tgit add lib &&\n+\t\tgit commit -m \"replace lib with another file\" &&\n+\t\tgit checkout main\n+\t) &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit fetch origin &&\n+\n+\t\tgit branch --delete-integrated origin/next >actual 2>&1 &&\n+\t\ttest_must_be_empty actual &&\n+\n+\t\tcheck_branches <<-\\EOF\n+\t\tmain\n+\t\treplaces-lib\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success '--delete-integrated reports a merge plainly next to an unmerged branch' '\n \tsetup_repo_for_delete_merged &&\n \tpush_topic a-unmerged unmerged-work &&\n\n"},{"id":"559010","messageId":"xmqqqzgfm7he.fsf@gitster.g","threadId":"66417","inReplyTo":"pull.2425.v9.git.git.1795184664.gitgitgadget@gmail.com","subject":"Re: [PATCH v9 0/4] branch: let --delete-merged find squash merged branches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-20T23:02:53Z","receivedAt":"2026-11-20T23:02:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Changes in v9:\n>\n>  * Fix --delete-integrated dying with \"unable to read blob object\" when a\n>    path was a submodule at the fork point and both the branch and upstream\n>    replaced it with different regular files. Such a branch is now kept.\n>  * Add a test for that case.\n\nThe changes between v8 and v9 looked OK (that does not say anything\nabout the changes up to v8, or the --delete-merged that was added to\nserve as a base of this series, though).\n\nThanks.\n\n"}]}