From: D. Ben Knoble Date: Thu, 22 Oct 2026 12:46:22 GMT Subject: Re: [RFC] branch: delete gone-upstream branches only when proven merged into a named ref Message-ID: In-Reply-To: On Fri, Oct 16, 2026 at 8:41 PM DonQuinleone wrote: > > 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! > 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. > 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). > (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= git branch -d --merged-into= etc. Or even "--merged=upstream:,into:". 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.