[PATCH v17 6/7] branch: add branch.<name>.deleteMerged opt-out
From: Harald Nordgren via GitGitGadget <hidden>
Date: 2026-06-22 07:30:01
Subsystem:
documentation, the rest · Maintainers:
Jonathan Corbet, Linus Torvalds
From: Harald Nordgren <redacted> Setting branch.<name>.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 <redacted> --- Documentation/config/branch.adoc | 7 +++++++ Documentation/git-branch.adoc | 5 +++-- builtin/branch.c | 15 +++++++++++++++ t/t3200-branch.sh | 26 ++++++++++++++++++++++++++ 4 files changed, 51 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.<name>.deleteMerged`:: + If set to `false`, branch _<name>_ 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 56ff889447..59ea3f471a 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 (`<branch>@{push}`) equals its upstream (`<branch>@{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.<name>.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 35fd3e9efc..5ea610efa1 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c@@ -758,6 +758,8 @@ static int delete_merged_branches(int argc, const char **argv, struct ref_array candidates = { 0 }; struct strset deletable = STRSET_INIT; struct strvec to_delete = STRVEC_INIT; + struct strbuf key = STRBUF_INIT; + bool quiet = flags & DELETE_BRANCH_QUIET; int i, ret = 0; if (!argc)
@@ -775,6 +777,7 @@ static int delete_merged_branches(int argc, const char **argv, const char *short_name; struct branch *branch; const char *upstream, *push; + int opt_out; if (!skip_prefix(full_name, "refs/heads/", &short_name)) BUG("filter returned non-branch ref '%s'", full_name);
@@ -793,6 +796,17 @@ static int delete_merged_branches(int argc, const char **argv, FILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED)) continue; + strbuf_reset(&key); + 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); + continue; + } + strset_add(&deletable, short_name); }
@@ -814,6 +828,7 @@ static int delete_merged_branches(int argc, const char **argv, DELETE_BRANCH_NO_HEAD_FALLBACK | flags); + strbuf_release(&key); strvec_clear(&to_delete); strset_clear(&deletable); ref_array_clear(&candidates);
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 1d372f95e8..b80d558b4a 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh@@ -1990,4 +1990,30 @@ test_expect_success '--delete-merged keeps a chain of upstreams of a kept branch test_cmp expect actual ' +test_expect_success '--delete-merged honours branch.<name>.deleteMerged=false' ' + test_when_finished "rm -rf repo" && + setup_repo_for_delete_merged && + merged_branch deleted origin/next && + merged_branch kept origin/next && + git -C repo config branch.kept.deleteMerged false && + git -C repo checkout --detach && + + git -C repo branch --delete-merged origin/next 2>err && + + test_grep "Skipping .kept." err && + test_must_fail git -C repo rev-parse --verify refs/heads/deleted && + git -C repo rev-parse --verify refs/heads/kept +' + +test_expect_success "branch -d still deletes a deleteMerged=false branch" ' + test_when_finished "rm -rf repo" && + setup_repo_for_delete_merged && + merged_branch kept origin/next && + git -C repo config branch.kept.deleteMerged false && + git -C repo checkout --detach && + + git -C repo branch -d kept && + test_must_fail git -C repo rev-parse --verify refs/heads/kept +' + test_done
--
gitgitgadget