Skip to content

Cannot delete head branch in worktree #14794

Open

Description

@heaths

Describe the bug

When you're in a worktree, gh pr merge -sd does not checkout the main branch. This is a follow up on #3442. I appreciate if the default branch is already checked out in another worktree/primary git will error and you can't do much about that, so report the error and terminate. But the assertion that worktrees are ephemeral needs to be challenged.

I have been using worktrees since they were first added, long before agentic coding. I keep several that I use in parallel for both human and agentic coding. gh pr merge -sd works great in the primary, but I have to remember to clean up orphaned local branches later and they collect fast.

I can appreciate that worktrees are likely more popular in agentic coding, but can we at least support a --force to delete the head branch locally and try to flip to the default branch? If it can't because git blocks it again, fine. Report the error and exit with an error code. But at least give me the choice to retain the same behavior. Or even make it a config setting so I can just default it.

Affected version

2.103.0

Steps to reproduce the behavior

  1. Within a repo, git worktree add -b feature1 ../azure-sdk-for-rust2
  2. Create and push a PR; when its checks pass and it's approved, gh pr merge -sd

Expected vs actual behavior

Expected

The local head branch is deleted and I'm (hopefully) checked out to the default branch if not already checked out elsewhere.

Actual

$ gh pr merge -sd
! Pull request Azure/azure-sdk-for-rust#5514 was already merged
! Branch update-zip is checked out in the current worktree (/home/heaths/src/azure-sdk-for-rust2); skipping local delete
  To finish cleanup, first navigate out of the worktree, then run:
  git worktree remove /home/heaths/src/azure-sdk-for-rust2 && git branch -D update-zip

Activity

  1. cli-triage commented on Oct 22, 2026

    @cli-triage

    I'm suggesting enhancement and gh-pr labels (for maintainer approval), not bug. This looks like intended-but-limited behavior rather than a defect: the current merge logic in merge.go lines 428-437 deliberately skips local branch deletion when the head branch is checked out in the current linked worktree, printing the exact warning quoted in the issue. That guard was added via #14007 (closing #3442) specifically to prevent git errors from deleting/checking-out a branch that's active in the current directory — it isn't that worktree cleanup was never considered, but that the implementation always bails out rather than attempting the base-branch checkout + branch delete the reporter wants (optionally gated by a --force flag or config setting). No duplicate found for this specific "add an opt-in to force the delete/checkout from within a worktree" request, though #14537 and #14550 cover related-but-distinct worktree/branch-deletion safety issues for gh pr merge --delete-branch. A first responder should weigh whether to add a flag/config to override this specific guard.

    Generated by Issue Triage (skills-driven) for #14794 · copilot · auto · 33.7 AIC · ⊞ 9.7K · ◷

  2. added
    enhancement
    gh-pr
    on Nov 2, 2026
  3. github-actions commented on Nov 2, 2026

    @github-actions
    Contributor

    Thank you for your issue! We have categorized it as an enhancement (feature or improvement) request, and it has been added to our backlog. In doing so, we are not committing to implementing this feature at this time, but, we will consider it for future releases based on community feedback and our own product roadmap.

    Unless you see the help wanted Contributions welcome label, we are not currently looking for external contributions for this feature.

    If you come across this issue and would like to see it implemented, please add a thumbs up! This will help us prioritize the feature. Please only comment if you have additional information or viewpoints to contribute.

  4. removed
    needs-triage
    on Nov 2, 2026
  5. mxmeinhold commented on Nov 19, 2026

    @mxmeinhold

    I am also running into this, I would really appreaciate a config option to allow me to keep my worktrees. I'll also note, gh pr merge -h states -d, --delete-branch[=true|false] Delete the local and remote branch after merge, which makes no mention of destructive actions upon worktrees

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions