Repository navigation
Description
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
- Within a repo,
git worktree add -b feature1 ../azure-sdk-for-rust2 - 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
cli-triage commentedon Oct 22, 2026
I'm suggesting
enhancementandgh-prlabels (for maintainer approval), notbug. 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 preventgiterrors 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--forceflag 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 forgh pr merge --delete-branch. A first responder should weigh whether to add a flag/config to override this specific guard.github-actions commentedon Nov 2, 2026
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 wantedContributions 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.
fix: the review's fixes
fix(ship): rebase-merge and delete the branch
fix: the review's fixes
mxmeinhold commentedon Nov 19, 2026
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 -hstates-d, --delete-branch[=true|false] Delete the local and remote branch after merge, which makes no mention of destructive actions upon worktrees