Skip to content

Instantly share code, notes, and snippets.

@jckw
Created January 15, 2026 20:00
Show Gist options
  • Select an option

  • Save jckw/dbcf8bf750af6a69253799b1feabaa72 to your computer and use it in GitHub Desktop.

Select an option

Save jckw/dbcf8bf750af6a69253799b1feabaa72 to your computer and use it in GitHub Desktop.
gfix - quickly create git worktrees from a PR number. Useful for resolving issues in AI generated code.
#!/bin/bash
#
# gfix - Open a PR branch in a new git worktree with Cursor
#
# This script makes it easy to work on PRs in isolated worktrees without
# switching branches or stashing changes in your main working directory.
# Each PR gets its own directory under .worktrees/ that you can work in
# independently.
#
# REQUIREMENTS:
# - gh (GitHub CLI) - install with: brew install gh
# - jq (JSON processor) - install with: brew install jq
# - cursor (Cursor IDE CLI) - should be available if Cursor is installed
#
# USAGE:
# gfix <pr-number> Create/open a worktree for the given PR
# gfix done <pr-number> Remove the worktree for the given PR
#
# EXAMPLES:
# gfix 566 Opens PR #566 in a new worktree at ../outro-turbo-worktrees/pr-566/
# gfix done 566 Removes the worktree for PR #566
#
# SETUP:
# Add to your PATH or create an alias in ~/.zshrc:
# export PATH="$PATH:/path/to/outro-turbo/scripts"
# Or:
# alias gfix="/path/to/outro-turbo/scripts/gfix"
#
set -e
REPO_ROOT="$(git rev-parse --show-toplevel)"
REPO_NAME="$(basename "$REPO_ROOT")"
WORKTREES_DIR="$(dirname "$REPO_ROOT")/${REPO_NAME}-worktrees"
# Handle "done" subcommand to remove worktree
if [ "$1" = "done" ]; then
if [ -z "$2" ]; then
echo "Usage: gfix done <pr-number>"
exit 1
fi
PR_NUMBER="$2"
WORKTREE_PATH="$WORKTREES_DIR/pr-$PR_NUMBER"
if [ ! -d "$WORKTREE_PATH" ]; then
echo "Error: No worktree found at $WORKTREE_PATH"
exit 1
fi
echo "Removing worktree for PR #$PR_NUMBER..."
git worktree remove "$WORKTREE_PATH" --force
echo "Done! Worktree removed."
exit 0
fi
# Main flow: create/open worktree
if [ -z "$1" ]; then
echo "Usage: gfix <pr-number>"
echo " gfix done <pr-number>"
exit 1
fi
PR_NUMBER="$1"
# Get PR info using gh CLI
echo "Fetching PR #$PR_NUMBER info..."
PR_INFO=$(gh pr view "$PR_NUMBER" --json headRefName,headRepository,headRepositoryOwner --jq '{branch: .headRefName, repo: .headRepository.name, owner: .headRepositoryOwner.login}')
BRANCH=$(echo "$PR_INFO" | jq -r '.branch')
OWNER=$(echo "$PR_INFO" | jq -r '.owner')
if [ -z "$BRANCH" ] || [ "$BRANCH" = "null" ]; then
echo "Error: Could not find PR #$PR_NUMBER"
exit 1
fi
echo "PR #$PR_NUMBER is on branch: $BRANCH"
# Create worktrees directory if it doesn't exist
mkdir -p "$WORKTREES_DIR"
WORKTREE_PATH="$WORKTREES_DIR/pr-$PR_NUMBER"
# Check if worktree already exists
if [ -d "$WORKTREE_PATH" ]; then
echo "Worktree already exists at $WORKTREE_PATH"
else
# Fetch the branch if it's from a fork
if [ "$OWNER" != "outro-health" ]; then
echo "Fetching from fork: $OWNER/$BRANCH"
git fetch "https://github.com/$OWNER/outro-turbo.git" "$BRANCH:$BRANCH" 2>/dev/null || true
else
# Fetch the latest from origin
echo "Fetching branch from origin..."
git fetch origin "$BRANCH" 2>/dev/null || true
fi
# Create the worktree
echo "Creating worktree at $WORKTREE_PATH..."
git worktree add "$WORKTREE_PATH" "$BRANCH"
fi
# Open Cursor in the worktree
echo "Opening Cursor..."
cursor "$WORKTREE_PATH"
echo "Done! Worktree for PR #$PR_NUMBER is ready at $WORKTREE_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment