claude-code - 💡(How to fix) Fix EnterWorktree: reuse existing worktree if one already exists for the branch [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#45024Fetched 2026-04-09 08:15:06
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Author
Participants
Timeline (top)
labeled ×2closed ×1
RAW_BUFFERClick to expand / collapse

Problem

When a git worktree already exists (e.g. created manually via git worktree add), EnterWorktree cannot switch into it. It only creates new worktrees with new branches based on HEAD.

Expected behavior

If a worktree already exists for the target branch/name, EnterWorktree should detect it and switch the session's working directory to the existing worktree instead of failing or creating a new one.

Use case

  1. User creates a worktree manually: git worktree add worktrees/my-branch my-branch
  2. User wants Claude Code to work in that worktree
  3. Currently there's no way to enter the existing worktree — EnterWorktree only creates new ones

Suggested behavior

  • Accept an optional branch or path parameter to target an existing worktree
  • If a worktree with the given name/branch already exists, switch into it rather than erroring

extent analysis

TL;DR

Modify the EnterWorktree function to accept an optional branch or path parameter to target and switch into existing worktrees.

Guidance

  • Review the EnterWorktree function to determine how it currently handles worktree creation and switching.
  • Consider adding a check to detect if a worktree with the given name/branch already exists before attempting to create a new one.
  • Update the function to accept an optional branch or path parameter to allow targeting of existing worktrees.
  • Implement logic to switch the session's working directory to the existing worktree if it is found.

Example

def EnterWorktree(branch=None, path=None):
    # Check if worktree already exists
    existing_worktree = git_get_worktree(branch, path)
    if existing_worktree:
        # Switch to existing worktree
        switch_to_worktree(existing_worktree)
    else:
        # Create new worktree
        create_new_worktree(branch, path)

Notes

The implementation details of git_get_worktree and switch_to_worktree are not provided, as they depend on the specific Git library or API being used.

Recommendation

Apply workaround by modifying the EnterWorktree function to accept an optional branch or path parameter and implement the suggested behavior to switch into existing worktrees. This will allow users to target and work with existing worktrees instead of creating new ones.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

If a worktree already exists for the target branch/name, EnterWorktree should detect it and switch the session's working directory to the existing worktree instead of failing or creating a new one.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING