branch: add --dry-run for --delete-merged
"git branch --dry-run --delete-merged ..." prints one line per ref that would be deleted without modifying refs or branch configuration. --dry-run is only meaningful together with --delete-merged and is rejected otherwise. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Harald Nordgren committed
Jul 22, 2026 at 07:10 UTC
0fe5f97c01850f05909a2011cecfcb7e6a23f22f
3 files changed
+62
-5
Documentation/git-branch.adoc
+7
-1
@@ -25,7 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch>
25
git branch (-c|-C) [<old-branch>] <new-branch>
26
git branch (-d|-D) [-r] <branch-name>...
27
git branch --edit-description [<branch-name>]
28
-git branch (--delete-merged <branch>)... [<pattern>...]
28
+git branch [--dry-run] (--delete-merged <branch>)... [<pattern>...]
29
30
DESCRIPTION
31
-----------
@@ -232,6 +232,12 @@ A branch that a surviving branch depends on through a chain of local
232
upstreams is kept, so a branch is never deleted out from under stacked
233
work.
234
235
+`--dry-run`::
236
+ With `--delete-merged`, print which branches would be
237
+ deleted and exit without touching any ref. Useful for
238
+ sanity-checking a wide pattern like `'origin/*'` before
239
+ committing to the deletion.
240
+
241
`-v`::
242
`-vv`::
243
`--verbose`::
builtin/branch.c
+18
-3
@@ -199,6 +199,7 @@ enum delete_branch_flags {
199
DELETE_BRANCH_QUIET = (1 << 1),
200
DELETE_BRANCH_SKIP_UNMERGED = (1 << 2),
201
DELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),
202
+ DELETE_BRANCH_DRY_RUN = (1 << 4),
203
};
204
205
static int check_branch_commit(const char *branchname, const char *refname,
@@ -340,13 +341,20 @@ static int delete_branches(int argc, const char **argv, int kinds,
341
free(target);
342
}
343
343
- if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
344
+ if (!(flags & DELETE_BRANCH_DRY_RUN) &&
345
+ refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
346
ret = 1;
347
348
for_each_string_list_item(item, &refs_to_delete) {
349
char *describe_ref = item->util;
350
char *name = item->string;
349
- if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
351
+ if (flags & DELETE_BRANCH_DRY_RUN) {
352
+ if (!(flags & DELETE_BRANCH_QUIET))
353
+ printf(remote_branch
354
+ ? _("Would delete remote-tracking branch %s (was %s).\n")
355
+ : _("Would delete branch %s (was %s).\n"),
356
+ name + branch_name_pos, describe_ref);
357
+ } else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
358
char *refname = name + branch_name_pos;
359
if (!(flags & DELETE_BRANCH_QUIET))
360
printf(remote_branch
@@ -900,6 +908,7 @@ int cmd_branch(int argc,
908
int delete = 0, rename = 0, copy = 0, list = 0,
909
unset_upstream = 0, show_current = 0, edit_description = 0;
910
struct strvec delete_merged = STRVEC_INIT;
911
+ int dry_run = 0;
912
const char *new_upstream = NULL;
913
int noncreate_actions = 0;
914
/* possible options */
@@ -956,6 +965,8 @@ int cmd_branch(int argc,
965
OPT_CALLBACK_F(0, "delete-merged", &delete_merged, N_("branch"),
966
N_("delete merged branches whose upstream matches <branch> (repeatable)"),
967
PARSE_OPT_NONEG, parse_opt_strvec),
968
+ OPT_BOOL(0, "dry-run", &dry_run,
969
+ N_("with --delete-merged, only print which branches would be deleted")),
970
OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
971
OPT_MERGED(&filter, N_("print only branches that are merged")),
972
OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -1018,6 +1029,9 @@ int cmd_branch(int argc,
1029
if (noncreate_actions > 1)
1030
usage_with_options(builtin_branch_usage, options);
1031
1032
+ if (dry_run && !delete_merged.nr)
1033
+ die(_("--dry-run requires --delete-merged"));
1034
+
1035
if (recurse_submodules_explicit) {
1036
if (!submodule_propagate_branches)
1037
die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -1058,7 +1072,8 @@ int cmd_branch(int argc,
1072
goto out;
1073
} else if (delete_merged.nr) {
1074
ret = delete_merged_branches(&delete_merged, argv,
1061
- quiet ? DELETE_BRANCH_QUIET : 0);
1075
+ (quiet ? DELETE_BRANCH_QUIET : 0) |
1076
+ (dry_run ? DELETE_BRANCH_DRY_RUN : 0));
1077
goto out;
1078
} else if (show_current) {
1079
print_current_branch_name();
t/t3200-branch.sh
+37
-1
@@ -1900,6 +1900,19 @@ test_expect_success '--delete-merged deletes only selected merged branches' '
1900
git checkout -b tracks-other other/main --track &&
1901
sha=$(git rev-parse --short merged) &&
1902
1903
+ git branch --dry-run --delete-merged origin/next merged >actual 2>&1 &&
1904
+ echo "Would delete branch merged (was $sha)." >expect &&
1905
+ test_cmp expect actual &&
1906
+ git rev-parse --verify refs/heads/merged &&
1907
+
1908
+ check_branches <<-\EOF &&
1909
+ also-merged
1910
+ main
1911
+ merged
1912
+ tracks-other
1913
+ unmerged
1914
+ EOF
1915
+
1916
git branch --delete-merged origin/next merged >actual 2>&1 &&
1917
echo "Deleted branch merged (was $sha)." >expect &&
1918
test_cmp expect actual &&
@@ -1948,9 +1961,12 @@ test_expect_success '--delete-merged keeps the upstream of a surviving branch' '
1961
git checkout -b topic feature --track &&
1962
git commit --allow-empty -m "topic work" &&
1963
1951
- git branch --delete-merged origin/next 2>err &&
1964
+ git branch --dry-run --delete-merged origin/next >out &&
1965
+ test_grep ! "feature" out &&
1966
1967
+ git branch --delete-merged origin/next 2>err &&
1968
test_must_be_empty err &&
1969
+
1970
check_branches <<-\EOF &&
1971
feature
1972
main
@@ -1978,6 +1994,21 @@ test_expect_success '--delete-merged keeps the upstream chain of a surviving bra
1994
git checkout -b tip mid --track &&
1995
git commit --allow-empty -m "tip work" &&
1996
1997
+ git branch --dry-run --delete-merged origin/next \
1998
+ --delete-merged lower >actual 2>&1 &&
1999
+ test_must_be_empty actual &&
2000
+
2001
+ git config --local --get-regexp "branch\\.(lower|mid|tip)\\.(merge|remote)" >actual &&
2002
+ cat >expect <<-\EOF &&
2003
+ branch.lower.remote origin
2004
+ branch.lower.merge refs/heads/next
2005
+ branch.mid.remote .
2006
+ branch.mid.merge refs/heads/lower
2007
+ branch.tip.remote .
2008
+ branch.tip.merge refs/heads/mid
2009
+ EOF
2010
+ test_cmp expect actual &&
2011
+
2012
git branch --delete-merged origin/next \
2013
--delete-merged lower >actual 2>&1 &&
2014
test_must_be_empty actual &&
@@ -2074,4 +2105,9 @@ test_expect_success "branch -d still deletes a deleteMerged=false branch" '
2105
)
2106
'
2107
2108
+test_expect_success '--dry-run without --delete-merged is rejected' '
2109
+ test_must_fail git -C forked branch --dry-run 2>err &&
2110
+ test_grep "requires --delete-merged" err
2111
+'
2112
+
2113
test_done