You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add checkout_branch command to switch to existing branches (#19)
## Summary
Add a new `checkout_branch()` method to create worktrees from existing
branches without creating new branches. This enables users to check out
branches created by others or on remote repositories directly into
worktrees.
## Key Changes
- **New `checkout_branch()` method** in `WorktreeManager`:
- Checks out an existing branch into a new worktree (no branch creation)
- Supports optional custom worktree names via `name` parameter
- Supports fetching from remote before checkout via `fetch` parameter
- Validates that branch exists locally or on remote
- Prevents duplicate worktrees for the same branch
- Stores worktree name for later retrieval by derived name
- **New `_derive_name_from_branch()` static method**:
- Extracts worktree name from branch name (e.g., `fix/login-bug` →
`login-bug`)
- Handles remote-tracking branches (strips `origin/` prefix)
- Takes the last path component of hierarchical branch names
- **Updated `list_worktrees()` method**:
- Now checks for stored worktree names first (set by
`checkout_branch()`)
- Falls back to extracting name from branch if no stored name exists
- Ensures worktrees created via `checkout_branch()` are findable by
derived name
- **New `fetch_branch()` git helper**:
- Fetches a specific branch from remote
- Used by `checkout_branch()` when `--fetch` flag is provided
- **CLI integration** (`wt switch -c -B`):
- Added `-B/--branch` option to `switch` command for checking out
existing branches
- Added `-f/--fetch` option to fetch before checkout
- Validates flag combinations (e.g., `-B` requires `-c`, cannot be used
with `-d` or `-b`)
- Comprehensive test coverage for all flag combinations and edge cases
## Implementation Details
- Worktree names are stored in git config (`worktree.<path>.name`) to
persist the derived name
- The `checkout_branch()` method uses `git worktree add` with
`create_branch=False` to avoid creating new branches
- Upstream tracking is configured automatically if the remote branch
exists
- Full backward compatibility maintained with existing worktree creation
methods
https://claude.ai/code/session_0189JpNR1U5W4vguS2XTetXg
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments