Re: [PATCH 2/2] branch: protect local upstreams from -d
- From
Elijah Newren <newren@gmail.com>
- Date
- Aug 27, 2026, 05:19 UTC
- Message-ID
- <CABPp-BHQvUwwA6v+5rq9=8iUWavDO1ScMVr-3ok4Zm5r_Bp2hQ@mail.gmail.com>
- In-Reply-To
- <d3d7a06e3d6f0c7adf9739ca496ed4012e261ac1.1787693117.git.gitgitgadget@gmail.com>
On Tue, Aug 25, 2026 at 2:30 PM Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> wrote:
Show 10 quoted lines
> > A local branch may be fully merged into its own upstream while still > serving as the base of a surviving stacked branch. Deleting it with > "git branch -d" then leaves the surviving branch with a missing > upstream. > > Use the existing stacked-branch protection after checking every > requested deletion. This makes multi-branch deletion independent of > argument order: a branch that fails its safety check remains available > to protect its upstream.
[...]
Show 12 quoted lines
> diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc > index bfdf459329..5c2a3339b2 100644 > --- a/Documentation/git-branch.adoc > +++ b/Documentation/git-branch.adoc > @@ -102,7 +102,9 @@ OPTIONS > `--delete`:: > Delete a branch. The branch must be fully merged in its > upstream branch, or in `HEAD` if no upstream was set with > - `--track` or `--set-upstream-to`. > + `--track` or `--set-upstream-to`, and must not be an upstream, > + directly or indirectly, of another local branch that will remain > + after the operation.
With this patch applied:
$ git init -q repo && cd repo $ git commit --allow-empty -m base [master (root-commit) b9a0882] base $ git branch A $ git branch B $ git branch C $ git branch --set-upstream-to=A B branch 'B' set up to track 'A'. $ git branch --set-upstream-to=B C branch 'C' set up to track 'B'. $ ~/floss/git-review/bin-wrappers/git branch -d A B error: the branch 'B' is an upstream of another branch hint: If you are sure you want to delete it, run 'git branch -D B' hint: Disable this message with "git config set advice.forceDeleteBranch false" Deleted branch A (was b9a0882).
So, C had B as an upstream and git did protect B from being deleted. That matches the claims above. However, B had A as an upstream and git didn't protect A; it deleted it. That doesn't match the claims above.
Verifying:
$ git config branch.B.merge refs/heads/A $ git rev-parse -q --verify refs/heads/A ; echo $? 1 $ git rev-parse -q --verify refs/heads/B ; echo $? b9a088270710b2494f5fa0668fc7e81a40aebd35 0