]> git.ipfire.org Git - thirdparty/git.git/commit
branch: add --prune-merged <branch>
authorHarald Nordgren <haraldnordgren@gmail.com>
Fri, 22 May 2026 11:31:36 +0000 (11:31 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 24 May 2026 08:41:07 +0000 (17:41 +0900)
commitaec7dbdccfd3b25139a886cb0c0f8102b3910289
tree2892cb0bb20f25f0efd9f9eaee132cfe78690d9d
parentdbc713fc04c9d75a6f7228a6abe3c682650399dd
branch: add --prune-merged <branch>

git branch --prune-merged <branch>...

deletes the local branches that "--forked <branch>" would list,
restricted to those whose tip is reachable from their configured
upstream: the work has already landed on the upstream they track,
so the local copy is no longer needed.

Reachability is read from the local refs only -- nothing is
fetched. Users who want fresh upstream refs run "git fetch" first;
the deletion path stays a separate, idempotent step that also
works offline.

Three classes of branches are spared:

  * any branch checked out in any worktree;
  * any branch whose upstream no longer resolves locally (its
    disappearance is not, on its own, evidence of integration);
  * any branch whose push destination equals its upstream
    (<branch>@{push} == <branch>@{upstream}). Such a branch
    cannot be distinguished from a freshly pulled trunk that
    just looks "fully merged" -- e.g. local "main" tracking and
    pushing to "origin/main" right after a pull. Only branches
    that push somewhere other than their upstream (typically
    topics in a fork-based workflow) are treated as candidates.

Deletion goes through the existing delete_branches() in warn-only
mode and with the HEAD-fallback disabled: a branch that is not
yet fully merged to its upstream is reported as a one-line warning
and skipped, so a single un-mergeable topic does not abort the
whole sweep, and there is no fallback to "merged into the
currently checked out branch" -- we only act on upstream-merged
status.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-branch.adoc
builtin/branch.c
t/t3200-branch.sh