[PATCH v9 4/5] branch: add branch.<name>.pruneMerged opt-out
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- May 13, 2026, 19:34 UTC
- Message-ID
- <19b6d94fa7ddb1d9907b36d2b6b6d6b44b089870.1778700883.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2285.v9.git.git.1778700883.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
Setting branch.<name>.pruneMerged=false exempts that branch from --prune-merged, even with --force. Useful for keeping a topic branch around between rounds.
Explicit deletion via 'git branch -d' is unaffected.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- Documentation/config/branch.adoc | 7 +++++++ Documentation/git-branch.adoc | 3 ++- builtin/branch.c | 23 +++++++++++++++++++++-- t/t3200-branch.sh | 27 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc index a4db9fa5c8..4662ef35c1 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>.pruneMerged`:: + If set to `false`, branch _<name>_ is exempt from + `git branch --prune-merged`. + Useful for topic branches 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 375a0a68da..7435d38447 100644 --- a/Documentation/git-branch.adoc +++ b/Documentation/git-branch.adoc @@ -227,7 +227,8 @@ against whatever the remote-tracking refs say locally. A branch whose upstream no longer resolves locally is left alone (its disappearance is not, on its own, evidence that the work was integrated). The currently checked-out branch in any worktree is -always preserved, as is the local branch that mirrors _<remote>_'s +always preserved, as is any branch with `branch.<name>.pruneMerged` +set to `false`, and the local branch that mirrors _<remote>_'s default branch. `-v`:: diff --git a/builtin/branch.c b/builtin/branch.c index 6fe2ffd7e8..bc4f4a4a18 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -835,12 +835,15 @@ static int prune_merged_branches(int argc, const char **argv, int quiet) for_each_string_list_item(item, &candidates) { const char *short_name = item->string; struct strbuf full = STRBUF_INIT; + struct strbuf key = STRBUF_INIT; struct branch *branch; const char *upstream; + int opt_out = 0; strbuf_addf(&full, "refs/heads/%s", short_name); if (branch_checked_out(full.buf)) { strbuf_release(&full); + strbuf_release(&key); continue; } strbuf_release(&full); @@ -849,13 +852,29 @@ static int prune_merged_branches(int argc, const char **argv, int quiet) upstream = branch ? branch_get_upstream(branch, NULL) : NULL; if (!upstream || !refs_ref_exists(get_main_ref_store(the_repository), - upstream)) + upstream)) { + strbuf_release(&key); continue; + } if (string_list_has_string(&protected_default_refs, upstream)) { const char *leaf = strrchr(upstream, '/'); - if (leaf && !strcmp(leaf + 1, short_name)) + if (leaf && !strcmp(leaf + 1, short_name)) { + strbuf_release(&key); continue; + } + } + + strbuf_addf(&key, "branch.%s.prunemerged", short_name); + if (!repo_config_get_bool(the_repository, key.buf, &opt_out) && + !opt_out) { + if (!quiet) + fprintf(stderr, _("Skipping '%s' " + "(branch.%s.pruneMerged 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 94ea493aee..885a275e36 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -1854,4 +1854,31 @@ test_expect_success '--prune-merged protects only the default branch by name, no test_must_fail git -C pm-default-alias rev-parse --verify refs/heads/trunk ' +test_expect_success '--prune-merged honours branch.<name>.pruneMerged=false' ' + test_when_finished "rm -rf pm-optout" && + git clone pm-upstream pm-optout && + 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 && + git -C pm-optout config branch.one.pruneMerged false && + + git -C pm-optout branch --prune-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 pruneMerged=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 && + git -C pm-optout-d config branch.one.pruneMerged 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