git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] branch: delete gone-upstream branches only when proven merged into a named ref

From
D. Ben Knoble <ben.knoble@gmail.com>
Date
Oct 22, 2026, 12:46 UTC
Message-ID
<CALnO6CAsCvJRywkw0WpO1nEhL+yU2-r_4Z2AHdJch1m=NK8=TA@mail.gmail.com>
In-Reply-To
<a8561fb7c2da446e7f7acc7e2a443991@donquinleone.com>
On Fri, Oct 16, 2026 at 8:41 PM DonQuinleone <don@donquinleone.com> wrote:
Show 41 quoted lines
>
> Hi all,
>
> This is my first time posting to the list, so good to meet everyone,
> and thanks in advance for bearing with me.
>
> I've just noticed that "git branch --delete-merged" is now available,
> and that it never deletes a topic in the workflow that hosting
> services (e.g. GitHub) encourage. Before writing a patch, I wanted to
> ask how the list might consider closing that gap.
>
> In that workflow, the topic tracks and pushes to a branch of its own
> name; when the pull request is merged with a merge commit, the server
> deletes that branch. With master as of c51408889:
>
>   $ git push -u origin topic
>        ... merged on the server, and origin's topic deleted ...
>
>   $ git fetch origin
>   $ git branch --dry-run --delete-merged 'origin/*'
>        ... spared, as pushing would update the upstream ...
>
>   $ git fetch --prune origin
>   $ git branch -vv
>   * master 61e9886 [origin/master: behind 2] base
>     topic  87d0c48 [origin/topic: gone] topic work
>   $ git branch --dry-run --delete-merged 'origin/*'
>        ... skipped, because the upstream is gone ...
>   $ git merge-base --is-ancestor topic origin/master && echo merged
>   merged
>
> So the branch is provably merged, but at no point in this workflow
> will --delete-merged remove it. A script reproducing this is at the
> bottom.
>
> From reading back through the archives, this is deliberate. When v7
> of the series fell back to the remote's default branch for a gone
> upstream, Junio wrote [1]:
>
> > If the upstream itself is gone, removing anything based on some other
> > criteria cannot by definition a "safety check".

Hm. I had a hard time following the "--delete-merged" series, so this may come out of ignorance, but: we are asking Git to delete things merged into "origin/*"; I'm not sure why we'd need to consult "topic@{upstream}" to see that "topic" is merged into "origin/master" here, since there's a merge commit with "topic" as one of the parents?!

Ah, but the manual seems to contradict my (personal) "natural" reading of the command: "--delete-merged" actually deletes branches whose tip is reachable from their upstream (when that upstream matches the given pattern). So, phooey. In this case, if "topic@{upstream}" was "origin/master", though, "--delete-merged=origin/*" would work, no? Incidentally, I find it a shame that GitHub's documentation encourages a triangular workflow (whether or not there is a fork remote involved) without talking about how to configure Git to help you: fork or not, if you push to a topic branch but merge to another, it's extremely handy to use push.default=current or some variant so that

- @{upstream} is the place you'll merge
- @{push} is the place you push

and suddenly "git pull [--rebase]" does the right thing, "git status" (esp. with the new "status.compareBranches") can tell you where you are relative to both places of interest, etc.

IOW, "git push -u origin topic" makes upstream configuration very odd in the GitHub-ish workflow!

Show 7 quoted lines
> I agree that a vanished upstream proves nothing, so I would like to
> propose separating selection from proof:
>
> "Upstream gone" may select a candidate, but it is never proof. The
> only proof is that the branch tip is reachable from a ref the user
> names explicitly, so nothing is inferred: neither HEAD, nor the
> remote's default branch.

I'll refrain from commenting on whether we want to make "--delete-merged" delete a branch here or whether we need an additional mechanism for including topics whose upstream disappeared, but Git's CLI is already a bit notorious, so whatever interface we design should be composable.

Show 13 quoted lines
> I can see two possible shapes.
>
> (A) A modifier on --delete-merged:
>
>   $ git branch --delete-merged 'origin/*' --into origin/master
>
> Branches whose upstream matches the pattern but no longer exists would
> become candidates, and would be deleted only if their tip is reachable
> from the named commit. check_branch_commit() would be handed that
> commit in place of HEAD, as suggested on v7 [1]. Branches whose
> upstream still exists would be judged exactly as today, and the
> worktree, stacked-branch and deleteMerged=false exclusions would still
> apply.

A standalone modifier for another option makes the interface (IMO) complex; now we have to explain that "--into" only makes sense in some contexts (and add code to bail out).

I would much rather see more --option=value,value,value syntaxes, like
     git branch --delete-merged='origin/*,into:origin/master'

If we gain another form, we don't need new --options. We would also be able to say "--delete-merged=into:origin" and DWIM without any upstream configuration (or, with push.default=simple, even when upstream is just the place one pushed to).

Show 10 quoted lines
> (B) Composable filters on -d:
>
>   $ git branch --upstream-gone --merged=origin/master      # preview
>   $ git branch -d --upstream-gone --merged=origin/master   # delete
>
> --upstream-gone would be useful on its own for listing (today the
> usual approach is to grep "git branch -vv" for ": gone]"). But it
> turns -d into a second bulk-delete mode beside --delete-merged, and
> --merged defaults to HEAD when it comes last, so a forgotten argument
> would quietly bring back HEAD as the proxy.

In a world where we didn't have --delete-merged, this would have been the natural fit, I think. Imagine:

    git branch -d --merged-upstream=<pat>
    git branch -d --merged-into=<pat>

etc. Or even "--merged=upstream:<pat>,into:<pat>". Then we could also take some of the "--list" filters and make them apply to "-d". In the ambiguous case of a ref named, say, "--contains", we could use "--" to separate options from named branches to delete.

Previous: DonQuinleoneNext: Harald Nordgren
Message 2 of 15 in “[RFC] branch: delete gone-upstream branches only when proven merged into a named ref”
  1. DonQuinleoneOct 17, 2026
  2. D. Ben KnobleOct 22, 2026
  3. Harald NordgrenOct 22, 2026
  4. D. Ben KnobleOct 24, 2026
  5. Harald NordgrenOct 24, 2026
  6. D. Ben KnobleOct 24, 2026
  7. Harald NordgrenOct 22, 2026
  8. Ben KnobleOct 22, 2026
  9. Harald NordgrenOct 22, 2026
  10. Harald NordgrenOct 22, 2026
  11. D. Ben KnobleOct 24, 2026
  12. DonQuinleoneOct 24, 2026
  13. Harald NordgrenOct 24, 2026
  14. D. Ben KnobleOct 27, 2026
  15. D. Ben KnobleOct 24, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.