[PATCH v16 7/7] branch: add --dry-run for --delete-merged
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Jun 18, 2026, 19:25 UTC
- Message-ID
- <6d5c52353e0bec2cba058f37b267a00a7b4f07ec.1781810729.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2285.v16.git.git.1781810729.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
With --dry-run, --delete-merged prints the local branches it would delete, one "Would delete branch <name>" line each, and exits without touching any ref. The same filtering applies, so the output is exactly the set that the real run would delete.
--dry-run is only meaningful together with --delete-merged and is rejected otherwise.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- Documentation/git-branch.adoc | 8 +++++++- builtin/branch.c | 9 ++++++++- t/t3200-branch.sh | 11 ++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc index 91700f2e8a..09063d74f2 100644 --- a/Documentation/git-branch.adoc +++ b/Documentation/git-branch.adoc @@ -25,7 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch> git branch (-c|-C) [<old-branch>] <new-branch> git branch (-d|-D) [-r] <branch-name>... git branch --edit-description [<branch-name>] -git branch --delete-merged <branch>... +git branch [--dry-run] --delete-merged <branch>... DESCRIPTION ----------- @@ -226,6 +226,12 @@ A branch whose work has not yet been merged into its upstream is silently skipped. Delete it with `git branch -D` if you want to remove it anyway. +`--dry-run`:: + With `--delete-merged`, print which branches would be + deleted and exit without touching any ref. Useful for + sanity-checking a wide pattern like `'origin/*'` before + committing to the deletion. + `-v`:: `-vv`:: `--verbose`:: diff --git a/builtin/branch.c b/builtin/branch.c index 942e2297c8..f67d0949fe 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -826,6 +826,7 @@ int cmd_branch(int argc, int delete = 0, rename = 0, copy = 0, list = 0, unset_upstream = 0, show_current = 0, edit_description = 0; int delete_merged = 0; + int dry_run = 0; const char *new_upstream = NULL; int noncreate_actions = 0; /* possible options */ @@ -881,6 +882,8 @@ int cmd_branch(int argc, N_("edit the description for the branch")), OPT_BOOL(0, "delete-merged", &delete_merged, N_("delete local branches whose upstream matches <branch> and are merged")), + OPT_BOOL(0, "dry-run", &dry_run, + N_("with --delete-merged, only print which branches would be deleted")), OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE), OPT_MERGED(&filter, N_("print only branches that are merged")), OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), @@ -943,6 +946,9 @@ int cmd_branch(int argc, if (noncreate_actions > 1) usage_with_options(builtin_branch_usage, options); + if (dry_run && !delete_merged) + die(_("--dry-run requires --delete-merged")); + if (recurse_submodules_explicit) { if (!submodule_propagate_branches) die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled")); @@ -983,7 +989,8 @@ int cmd_branch(int argc, goto out; } else if (delete_merged) { ret = delete_merged_branches(argc, argv, - quiet ? DELETE_BRANCH_QUIET : 0); + (quiet ? DELETE_BRANCH_QUIET : 0) | + (dry_run ? DELETE_BRANCH_DRY_RUN : 0)); goto out; } else if (show_current) { print_current_branch_name(); diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 09cecfaff5..4a71845c76 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -1892,8 +1892,12 @@ test_expect_success '--delete-merged deletes merged branches and spares the rest ) && sha=$(git -C repo rev-parse --short merged) && - git -C repo branch --delete-merged origin/next >actual 2>&1 && + git -C repo branch --dry-run --delete-merged origin/next >actual 2>&1 && + echo "Would delete branch merged (was $sha)." >expect && + test_cmp expect actual && + git -C repo rev-parse --verify refs/heads/merged && + git -C repo branch --delete-merged origin/next >actual 2>&1 && echo "Deleted branch merged (was $sha)." >expect && test_cmp expect actual && git -C repo for-each-ref --format="%(refname:short)" refs/heads/ >actual && @@ -1970,4 +1974,9 @@ test_expect_success "branch -d still deletes a deleteMerged=false branch" ' test_must_fail git -C repo rev-parse --verify refs/heads/kept ' +test_expect_success '--dry-run without --delete-merged is rejected' ' + test_must_fail git -C forked branch --dry-run 2>err && + test_grep "requires --delete-merged" err +' + test_done
-- gitgitgadget