[PATCH v2 4/6] fetch: add --prune-merged
From: Harald Nordgren via GitGitGadget <hidden>
Date: 2026-05-04 18:27:38
Subsystem:
documentation, the rest · Maintainers:
Jonathan Corbet, Linus Torvalds
From: Harald Nordgren <redacted> After a successful fetch from a configured remote, run 'git branch --prune-merged <remote>' to delete local branches whose push destination ref has just been pruned. Signed-off-by: Harald Nordgren <redacted> --- Documentation/fetch-options.adoc | 8 ++++++++ builtin/fetch.c | 20 ++++++++++++++++++++ t/t5510-fetch.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+)
diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
index 81a9d7f9bb..d863a9184e 100644
--- a/Documentation/fetch-options.adoc
+++ b/Documentation/fetch-options.adoc@@ -185,6 +185,14 @@ See the PRUNING section below for more details. + See the PRUNING section below for more details. +`--prune-merged`:: + After a successful fetch, run `git branch --prune-merged + <remote>` for the fetched remote, deleting local branches + that fork from this remote and whose tip is reachable from + their upstream remote-tracking ref. See linkgit:git-branch[1] + for the exact selection rules. The currently checked-out + branch is always preserved. + endif::git-pull[] ifndef::git-pull[]
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a22c319467..5451bf3b5b 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c@@ -82,6 +82,8 @@ static int prune = -1; /* unspecified */ static int prune_tags = -1; /* unspecified */ #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */ +static int prune_merged; + static int append, dry_run, force, keep, update_head_ok; static int write_fetch_head = 1; static int verbosity, deepen_relative, set_upstream, refetch;
@@ -2189,6 +2191,8 @@ static void add_options_to_argv(struct strvec *argv, strvec_push(argv, prune ? "--prune" : "--no-prune"); if (prune_tags != -1) strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags"); + if (prune_merged) + strvec_push(argv, "--prune-merged"); if (update_head_ok) strvec_push(argv, "--update-head-ok"); if (force)
@@ -2382,6 +2386,15 @@ static inline void fetch_one_setup_partial(struct remote *remote, return; } +static int prune_merged_for_remote(const struct remote *remote) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + + cmd.git_cmd = 1; + strvec_pushl(&cmd.args, "branch", "--prune-merged", remote->name, NULL); + return run_command(&cmd); +} + static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok, int use_stdin_refspecs, const struct fetch_config *config,
@@ -2457,6 +2470,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, refspec_clear(&rs); transport_disconnect(gtransport); gtransport = NULL; + + if (!exit_code && prune_merged && remote_via_config && + prune_merged_for_remote(remote)) + exit_code = 1; + return exit_code; }
@@ -2520,6 +2538,8 @@ int cmd_fetch(int argc, N_("prune remote-tracking branches no longer on remote")), OPT_BOOL('P', "prune-tags", &prune_tags, N_("prune local tags no longer on remote and clobber changed tags")), + OPT_BOOL(0, "prune-merged", &prune_merged, + N_("after pruning, also delete local branches forked from this remote whose tips are reachable from their upstream")), OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"), N_("control recursive fetching of submodules"), PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index 6fe21e2b3a..b94fd2bda0 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh@@ -386,6 +386,37 @@ test_expect_success REFFILES 'fetch --prune fails to delete branches' ' ) ' +test_expect_success 'fetch --prune-merged: setup' ' + git init -b main fetch-pm-parent && + test_commit -C fetch-pm-parent base +' + +test_expect_success 'fetch --prune-merged deletes merged local branches' ' + test_when_finished "rm -rf fetch-pm-clone" && + git -C fetch-pm-parent branch one base && + git clone fetch-pm-parent fetch-pm-clone && + git -C fetch-pm-clone branch one --track origin/one && + git -C fetch-pm-parent branch -D one && + + git -C fetch-pm-clone fetch --prune --prune-merged origin && + + test_must_fail git -C fetch-pm-clone rev-parse --verify refs/heads/one +' + +test_expect_success 'fetch --prune-merged skips unmerged local branches' ' + test_when_finished "rm -rf fetch-pm-unmerged" && + git -C fetch-pm-parent branch two base && + git clone fetch-pm-parent fetch-pm-unmerged && + git -C fetch-pm-unmerged checkout -b two --track origin/two && + test_commit -C fetch-pm-unmerged unpushed && + git -C fetch-pm-unmerged checkout - && + git -C fetch-pm-parent branch -D two && + + git -C fetch-pm-unmerged fetch --prune --prune-merged origin 2>err && + test_grep "not fully merged" err && + git -C fetch-pm-unmerged rev-parse --verify refs/heads/two +' + test_expect_success 'fetch --atomic works with a single branch' ' test_when_finished "rm -rf atomic" &&
--
gitgitgadget