Skip to content

Conversation

@silouanwright
Copy link

@silouanwright silouanwright commented Sep 18, 2025

Summary

This PR includes two important improvements to the gh worktree pr command:

1. Fix: Checkout correct PR branch (bug fix)

This fixes a critical bug in the original implementation. The command was not checking out the actual PR branch - instead it was creating a new local branch. This meant users weren't actually getting the PR's code when running gh worktree pr.

Before (broken):

  • gh worktree pr 123 → Creates new branch named after the path
  • Git command used: git worktree add <path> (missing branch parameter)
  • Result: Empty worktree on wrong branch, no PR commits visible

After (fixed):

  • gh worktree pr 123 → Checks out the actual PR branch
  • Git command used: git worktree add <path> <branch>
  • Result: Worktree contains the actual PR commits

2. Enhancement: Use exact paths by default

Changes the default behavior when providing a custom path to use the exact path specified, rather than appending the branch name as a subdirectory. The previous behavior is still available via the new --append-branch flag.

Before:

  • gh worktree pr 123 ../pr-123 → Creates at ../pr-123/branch-name

After:

  • gh worktree pr 123 ../pr-123 → Creates at ../pr-123 (exact path)
  • gh worktree pr 123 ../pr-123 --append-branch → Creates at ../pr-123/branch-name (old behavior)

Changes

  • Fixed worktree.Add() to pass branch name to git worktree add command
  • Modified path handling to use exact paths by default when provided
  • Added --append-branch flag to preserve the old subdirectory behavior
  • Created AddWithOptions() function to handle both behaviors

Testing

Both fixes have been tested locally:

  • PR worktrees now correctly show the PR's commits and branch
  • Path handling works as expected with and without the --append-branch flag

Breaking Change

⚠️ The path handling change is breaking for users who rely on automatic branch name appending. They will need to add the --append-branch flag to maintain the previous behavior.

However, the bug fix for branch checkout is purely beneficial - it makes the tool actually work as intended.

When a custom path is provided to 'gh worktree pr', use it directly
instead of appending the branch name as a subdirectory. This allows
for cleaner worktree organization.
BREAKING CHANGE: When providing a custom path to 'gh worktree pr', it now uses
that path exactly as provided by default. The previous behavior of appending
the branch name as a subdirectory is now available via the --append-branch flag.

This change makes the tool behave more intuitively - when users specify a path,
they expect that exact path to be used, similar to other git/unix commands.

Examples:
- gh worktree pr 123 ../pr-123 → Creates at ../pr-123
- gh worktree pr 123 ../pr-123 --append-branch → Creates at ../pr-123/branch-name
The original implementation was missing the branch parameter when calling
'git worktree add', which caused it to create a new local branch instead
of checking out the actual PR branch. This fix ensures the PR's branch
is properly checked out in the worktree.
- Add pre-checks to detect existing worktrees before creation attempt
- Check if target directory already exists with clear messaging
- Parse git errors to provide user-friendly messages
- Support detection of both branch worktrees and detached HEAD worktrees
- Provide actionable error messages with specific guidance for resolution
- Automatically removes worktrees for merged/closed PRs
- Identifies stale worktrees (30+ days without commits)
- Interactive prompt for selective stale worktree removal
- Supports dry-run mode to preview changes
- Configurable stale threshold via --stale-days flag
- Extracts PR numbers from common branch naming patterns
- Updates README with command documentation
- Extract PR numbers from worktree directory names (e.g., web-frontend-pr-1018)
- Fix pattern matching to prioritize PR-specific patterns over generic numbers
- Ensure PR detection works for both branch names and directory names
- Use more specific regex patterns to avoid false positives
@eikster-dk
Copy link
Owner

thank you @silouanwright, I think I missed this PR in my notifications 🙈

I will set aside time to review and test it this week.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants