Skip to content

feat(merge): handle worktree context in gh pr merge --delete-branch #13380

@devseunggwan

Description

@devseunggwan

Problem

When running gh pr merge --squash --delete-branch from within a git worktree (not the main worktree), the command creates a partial failure state:

  1. The GitHub API merge succeeds (PR is merged)
  2. The local git checkout <base-branch> fails because the base branch is already checked out in the main worktree

Error message:

fatal: '<base>' is already used by worktree at '/path/to/main-worktree'

The result is a confusing state where:

  • The PR is merged on GitHub
  • Local cleanup (branch deletion) did not happen
  • Re-running the command says "PR already merged" + "cannot delete branch used by worktree"

Expected Behavior

One of the following improvements would resolve this:

Option A: Add a --no-local / --remote-only flag to skip local git operations after merge, for users who prefer to handle local cleanup separately.

Option B: Detect when the base branch is checked out in another worktree and skip the local git checkout <base> step automatically (since it's not strictly necessary for cleanup).

Option C: When the local git operations fail after a successful API merge, emit a clearer message explaining that the PR was merged but local cleanup failed, and suggest the manual cleanup steps.

Current Workaround

Run from the main worktree directory (where the base branch is checked out) rather than from within the feature worktree. Or: remove the worktree first, then run gh pr merge.

Environment

  • gh version: latest
  • Scenario: git worktree workflow (feature branch checked out in a separate worktree directory while main is checked out in the primary worktree)

Metadata

Metadata

Assignees

No one assigned

    Labels

    gh-prrelating to the gh pr command

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions