Syntax
Arguments
<number>(required): The GitHub pull request number
Requirements
GitHub CLI
This command requires the GitHub CLI (gh) to be installed and authenticated:
functions/worktree.zsh:129-132
GitHub Repository
The repository must:- Have a GitHub remote configured
- Use GitHub as the origin remote
- Be accessible with your GitHub authentication
Behavior
Thepr command creates a worktree from a pull request with these steps:
1. GitHub CLI Validation
First, checks ifgh CLI is available:
- Searches for
ghcommand in PATH - Displays error if not found
- Requires installation before proceeding
functions/worktree.zsh:129-132
2. Repository Information
Retrieves repository details:- Gets the origin remote URL from the bare repository
- Converts URL to
OWNER/REPOformat for GitHub CLI - Handles both SSH and HTTPS remote URLs
functions/worktree.zsh:139-144
3. Pull Request Branch Resolution
Fetches PR details using GitHub CLI:- Queries GitHub API for PR #
<number> - Extracts the head branch name (source branch of PR)
- Validates that the PR exists and is accessible
functions/worktree.zsh:146-152
4. Existing Worktree Check
Before fetching, checks if worktree already exists:- Uses the PR’s branch name to construct worktree path
- If worktree exists, displays message and runs post-add hooks
- Avoids re-fetching if worktree is already present
functions/worktree.zsh:156-160
5. Fetch PR Branch
Fetches the PR branch from GitHub:- Uses Git’s pull request ref:
pull/<number>/head - Creates a local branch with the PR’s branch name
- Downloads all commits from the PR
functions/worktree.zsh:162-165
6. Create Worktree
Creates a worktree for the PR branch:- Worktree path:
<repo-root>/<branch-name>/ - Checks out the fetched PR branch
- Sets up the working directory
functions/worktree.zsh:167-170
7. Tracking Configuration
Attempts to set up remote tracking:- Tries to configure
origin/<branch>as upstream - Silently continues if upstream branch doesn’t exist
- Useful if PR branch exists on origin
functions/worktree.zsh:172
8. Post-Add Hooks
After successful creation:- Calls
post_wt_addhook with worktree path - May navigate to the PR worktree
- May display PR information
functions/worktree.zsh:174-175
Examples
Review Pull Request #42
functions/worktree.zsh:174
Review and Test
Multiple PR Reviews
Cleanup After Review
Error Cases
Missing PR Number
functions/worktree.zsh:123-127
GitHub CLI Not Installed
functions/worktree.zsh:129-132
PR Not Found
- The PR number doesn’t exist
- The PR is in a different repository
- You don’t have access to the PR
- The PR has been deleted
functions/worktree.zsh:149-152
Failed to Fetch PR
- Network connectivity issues
- GitHub authentication problems
- Repository access restrictions
- Deleted or closed PR branch
functions/worktree.zsh:162-165
Failed to Create Worktree
- The worktree directory already exists but isn’t tracked
- Filesystem permissions prevent directory creation
- Disk space is insufficient
functions/worktree.zsh:167-170
Repository Root Not Found
functions/worktree.zsh:135-136
Tips and Best Practices
Install GitHub CLI First
Set upgh CLI before using this command:
Finding PR Numbers
Get PR numbers from:- GitHub web interface
- GitHub CLI:
gh pr list - GitHub notifications
- PR URLs:
github.com/owner/repo/pull/123→ number is123
Review Workflow
Efficient PR review process:Parallel Reviews
Review multiple PRs without context switching:Branch Naming
The worktree uses the PR’s source branch name:- If PR is from
feature/new-api, worktree isfeature/new-api - Branch names may include slashes
- Use
repo wt listto see exact branch names
Existing Worktrees
If you runrepo wt pr on a PR whose worktree already exists:
- No error occurs
- Worktree is not recreated
- Post-add hooks still run
- Useful for re-entering an existing PR worktree
functions/worktree.zsh:156-160
Upstream Tracking
The command attempts to set upstream tracking:- Enables
git pullto fetch PR updates - Works if the PR branch exists on origin
- Silently skips if branch is fork-based or doesn’t exist remotely
functions/worktree.zsh:172
Fork-Based PRs
For PRs from forks:- The command still works
- Fetches from the PR ref directly
- Upstream tracking may not be configured
- You can still review and test the code
Updating PR Worktrees
If the PR is updated after creating the worktree:Integration with GitHub CLI
Combine with othergh commands:
See Also
- repo wt add - Create regular worktrees
- repo wt list - View all worktrees
- repo wt go - Navigate to worktrees
- repo wt rm - Remove worktrees
- GitHub CLI Documentation - Learn more about
gh