Files
wehub-resource-sync bb5c75ce05
Component Security Validation / Security Audit (push) Has been cancelled
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:58 +08:00

1 line
5.4 KiB
JSON

{"content": "# Git Worktree Commands\n\n## Create Worktrees for All Open PRs\n\nThis command fetches all open pull requests using GitHub CLI, then creates a git worktree for each PR's branch in the `./tree/<BRANCH_NAME>` directory.\n\n```bash\n# Ensure GitHub CLI is installed and authenticated\ngh auth status || (echo \"Please run 'gh auth login' first\" && exit 1)\n\n# Create the tree directory if it doesn't exist\nmkdir -p ./tree\n\n# List all open PRs and create worktrees for each branch\ngh pr list --json headRefName --jq '.[].headRefName' | while read branch; do\n # Handle branch names with slashes (like \"feature/foo\")\n branch_path=\"./tree/${branch}\"\n \n # For branches with slashes, create the directory structure\n if [[ \"$branch\" == */* ]]; then\n dir_path=$(dirname \"$branch_path\")\n mkdir -p \"$dir_path\"\n fi\n\n # Check if worktree already exists\n if [ ! -d \"$branch_path\" ]; then\n echo \"Creating worktree for $branch\"\n git worktree add \"$branch_path\" \"$branch\"\n else\n echo \"Worktree for $branch already exists\"\n fi\ndone\n\n# Display all created worktrees\necho \"\\nWorktree list:\"\ngit worktree list\n```\n\n### Example Output\n\n```\nCreating worktree for fix-bug-123\nHEAD is now at a1b2c3d Fix bug 123\nCreating worktree for feature/new-feature\nHEAD is now at e4f5g6h Add new feature\nWorktree for documentation-update already exists\n\nWorktree list:\n/path/to/repo abc1234 [main]\n/path/to/repo/tree/fix-bug-123 a1b2c3d [fix-bug-123]\n/path/to/repo/tree/feature/new-feature e4f5g6h [feature/new-feature]\n/path/to/repo/tree/documentation-update d5e6f7g [documentation-update]\n```\n\n### Cleanup Stale Worktrees (Optional)\n\nYou can add this to remove stale worktrees for branches that no longer exist:\n\n```bash\n# Get current branches\ncurrent_branches=$(git branch -a | grep -v HEAD | grep -v main | sed 's/^[ *]*//' | sed 's|remotes/origin/||' | sort | uniq)\n\n# Get existing worktrees (excluding main worktree)\nworktree_paths=$(git worktree list | tail -n +2 | awk '{print $1}')\n\nfor path in $worktree_paths; do\n # Extract branch name from path\n branch_name=$(basename \"$path\")\n \n # Skip special cases\n if [[ \"$branch_name\" == \"main\" ]]; then\n continue\n fi\n \n # Check if branch still exists\n if ! echo \"$current_branches\" | grep -q \"^$branch_name$\"; then\n echo \"Removing stale worktree for deleted branch: $branch_name\"\n git worktree remove --force \"$path\"\n fi\ndone\n```\n\n## Create New Branch and Worktree\n\nThis interactive command creates a new git branch and sets up a worktree for it:\n\n```bash\n#!/bin/bash\n\n# Ensure we're in a git repository\nif ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then\n echo \"Error: Not in a git repository\"\n exit 1\nfi\n\n# Get the repository root\nrepo_root=$(git rev-parse --show-toplevel)\n\n# Prompt for branch name\nread -p \"Enter new branch name: \" branch_name\n\n# Validate branch name (basic validation)\nif [[ -z \"$branch_name\" ]]; then\n echo \"Error: Branch name cannot be empty\"\n exit 1\nfi\n\nif git show-ref --verify --quiet \"refs/heads/$branch_name\"; then\n echo \"Warning: Branch '$branch_name' already exists\"\n read -p \"Do you want to use the existing branch? (y/n): \" use_existing\n if [[ \"$use_existing\" != \"y\" ]]; then\n exit 1\n fi\nfi\n\n# Create branch directory\nbranch_path=\"$repo_root/tree/$branch_name\"\n\n# Handle branch names with slashes (like \"feature/foo\")\nif [[ \"$branch_name\" == */* ]]; then\n dir_path=$(dirname \"$branch_path\")\n mkdir -p \"$dir_path\"\nfi\n\n# Make sure parent directory exists\nmkdir -p \"$(dirname \"$branch_path\")\"\n\n# Check if a worktree already exists\nif [ -d \"$branch_path\" ]; then\n echo \"Error: Worktree directory already exists: $branch_path\"\n exit 1\nfi\n\n# Create branch and worktree\nif git show-ref --verify --quiet \"refs/heads/$branch_name\"; then\n # Branch exists, create worktree\n echo \"Creating worktree for existing branch '$branch_name'...\"\n git worktree add \"$branch_path\" \"$branch_name\"\nelse\n # Create new branch and worktree\n echo \"Creating new branch '$branch_name' and worktree...\"\n git worktree add -b \"$branch_name\" \"$branch_path\"\nfi\n\necho \"Success! New worktree created at: $branch_path\"\necho \"To start working on this branch, run: cd $branch_path\"\n```\n\n### Example Usage\n\n```\n$ ./create-branch-worktree.sh\nEnter new branch name: feature/user-authentication\nCreating new branch 'feature/user-authentication' and worktree...\nPreparing worktree (creating new branch 'feature/user-authentication')\nHEAD is now at abc1234 Previous commit message\nSuccess! New worktree created at: /path/to/repo/tree/feature/user-authentication\nTo start working on this branch, run: cd /path/to/repo/tree/feature/user-authentication\n```\n\n### Creating a New Branch from a Different Base\n\nIf you want to start your branch from a different base (not the current HEAD), you can modify the script:\n\n```bash\nread -p \"Enter new branch name: \" branch_name\nread -p \"Enter base branch/commit (default: HEAD): \" base_commit\nbase_commit=${base_commit:-HEAD}\n\n# Then use the specified base when creating the worktree\ngit worktree add -b \"$branch_name\" \"$branch_path\" \"$base_commit\"\n```\n\nThis will allow you to specify any commit, tag, or branch name as the starting point for your new branch."}