[PATCH v18 6/7] branch: add branch.<name>.deleteMerged opt-out
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Jun 24, 2026, 21:55 UTC
- Message-ID
- <d52d717b70c80b668e6d3a1fdf186ab4846664c7.1782338106.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2285.v18.git.git.1782338106.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
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 <haraldnordgren@gmail.com> --- 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 66b1c87c55..d482cded3d 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 d12a2f57ea..bce85cb52e 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -781,8 +781,10 @@ 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; struct hashmap_iter iter; struct strmap_entry *entry; + bool quiet = flags & DELETE_BRANCH_QUIET; int i, ret = 0; if (!argc) @@ -800,6 +802,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); @@ -818,6 +821,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); } @@ -833,6 +847,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 047ba54778..b7595610d9 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -2024,4 +2024,30 @@ test_expect_success '--delete-merged clears the upstream of a kept base whose ow 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