From: Harald Nordgren via GitGitGadget Date: Mon, 15 Jun 2026 16:47:21 GMT Subject: [PATCH v15 6/7] branch: add branch..deleteMerged opt-out Message-ID: <72aaca0666457a29705de9525ee0210b4fc5805f.1781542042.git.gitgitgadget@gmail.com> In-Reply-To: From: Harald Nordgren Setting branch..deleteMerged=false exempts that branch from "git branch --delete-merged", which is useful for a topic you want to keep developing after an early round of it has been merged upstream. Unless --quiet is given, each skip is reported so the user knows why their topic was kept. Explicit deletion with "git branch -d" still uses the normal merge check and ignores this setting. Signed-off-by: Harald Nordgren --- Documentation/config/branch.adoc | 7 +++++++ Documentation/git-branch.adoc | 5 +++-- builtin/branch.c | 14 ++++++++++++++ t/t3200-branch.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc index a4db9fa5c8..d8483acb4f 100644 --- a/Documentation/config/branch.adoc +++ b/Documentation/config/branch.adoc @@ -102,3 +102,10 @@ for details). `git branch --edit-description`. Branch description is automatically added to the `format-patch` cover letter or `request-pull` summary. + +`branch..deleteMerged`:: + If set to `false`, branch __ is exempt from + `git branch --delete-merged`. Useful for a topic branch you + intend to develop further after an initial round has been + merged upstream. Defaults to true. Explicit deletion via + `git branch -d` is unaffected. diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc index f82cfa36d0..91700f2e8a 100644 --- a/Documentation/git-branch.adoc +++ b/Documentation/git-branch.adoc @@ -215,10 +215,11 @@ A branch is not deleted when: + -- * its upstream remote-tracking branch no longer exists, -* it is checked out in any worktree, or +* it is checked out in any worktree, * its push destination (`@{push}`) equals its upstream (`@{upstream}`), so it cannot be distinguished from a - branch that just looks "fully merged" right after a pull. + branch that just looks "fully merged" right after a pull, or +* `branch..deleteMerged` is set to `false`. -- + A branch whose work has not yet been merged into its upstream is diff --git a/builtin/branch.c b/builtin/branch.c index f01e03cc26..0e1e7c2e6f 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -739,6 +739,8 @@ static int delete_merged_branches(int argc, const char **argv, const char *short_name; struct branch *branch; const char *upstream, *push; + struct strbuf key = STRBUF_INIT; + int opt_out; if (!skip_prefix(full_name, "refs/heads/", &short_name)) BUG("filter returned non-branch ref '%s'", full_name); @@ -753,6 +755,18 @@ static int delete_merged_branches(int argc, const char **argv, if (!push || !strcmp(push, upstream)) continue; + strbuf_addf(&key, "branch.%s.deletemerged", short_name); + if (!repo_config_get_bool(the_repository, key.buf, &opt_out) && + !opt_out) { + if (!quiet) + fprintf(stderr, + _("Skipping '%s' (branch.%s.deleteMerged is false)\n"), + short_name, short_name); + strbuf_release(&key); + continue; + } + strbuf_release(&key); + strvec_push(&deletable, short_name); } diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index b74e119d3b..5ac3c2bb5d 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -2030,4 +2030,34 @@ test_expect_success '--delete-merged takes positional arguments' ' test_must_fail git -C pm-positional rev-parse --verify refs/heads/two ' +test_expect_success '--delete-merged honours branch..deleteMerged=false' ' + test_when_finished "rm -rf pm-optout" && + git clone pm-upstream pm-optout && + git -C pm-optout remote add fork ../pm-fork && + test_config -C pm-optout remote.pushDefault fork && + test_config -C pm-optout push.default current && + git -C pm-optout branch one one-commit && + git -C pm-optout branch --set-upstream-to=origin/next one && + git -C pm-optout branch two two-commit && + git -C pm-optout branch --set-upstream-to=origin/next two && + test_config -C pm-optout branch.one.deleteMerged false && + + git -C pm-optout branch --delete-merged "origin/*" 2>err && + + git -C pm-optout rev-parse --verify refs/heads/one && + test_must_fail git -C pm-optout rev-parse --verify refs/heads/two && + test_grep "Skipping .one." err +' + +test_expect_success 'branch -d still deletes a deleteMerged=false branch' ' + test_when_finished "rm -rf pm-optout-d" && + git clone pm-upstream pm-optout-d && + git -C pm-optout-d branch one one-commit && + git -C pm-optout-d branch --set-upstream-to=origin/next one && + test_config -C pm-optout-d branch.one.deleteMerged false && + + git -C pm-optout-d branch -d one && + test_must_fail git -C pm-optout-d rev-parse --verify refs/heads/one +' + test_done -- gitgitgadget