Created
November 14, 2025 15:12
-
-
Save BSVogler/4af4967366be14b02e594602b0a65f9b to your computer and use it in GitHub Desktop.
Git Worktree Creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/zsh | |
| # | |
| # gwt - Git Worktree Creator with Virtual Environment Copying | |
| # | |
| # Creates a git worktree for a branch, copies gitignored virtual environments | |
| # (.venv and node_modules), and starts Claude Code in the new worktree. | |
| # | |
| # Usage: gwt <branch-name> | |
| # | |
| if [ $# -eq 0 ]; then | |
| echo "Usage: gwt <branch-name>" | |
| echo "" | |
| echo "Creates a git worktree, copies virtual environments, and starts Claude Code" | |
| echo "" | |
| echo "Example:" | |
| echo " gwt feature/new-api # Creates worktree for feature/new-api branch" | |
| return 1 | |
| fi | |
| branch_name="$1" | |
| current_dir="$(pwd)" | |
| # Check if we're in a git repo | |
| if ! git rev-parse --git-dir > /dev/null 2>&1; then | |
| echo "Error: Not in a git repository" | |
| return 1 | |
| fi | |
| # Get the root of the git repo | |
| repo_root="$(git rev-parse --show-toplevel)" | |
| repo_name="$(basename "$repo_root")" | |
| # Determine worktree directory (sibling to current repo) | |
| worktree_path="${repo_root}/../${repo_name}-${branch_name}" | |
| # Check if branch exists, if not create it | |
| if ! git rev-parse --verify "$branch_name" > /dev/null 2>&1; then | |
| echo "Branch '$branch_name' doesn't exist. Creating it..." | |
| git branch "$branch_name" | |
| fi | |
| # Create the worktree | |
| echo "Creating worktree at: $worktree_path" | |
| if ! git worktree add "$worktree_path" "$branch_name"; then | |
| echo "Error: Failed to create worktree" | |
| return 1 | |
| fi | |
| # Copy virtual environments | |
| echo "" | |
| echo "Copying virtual environments..." | |
| if [ -d "$repo_root/.venv" ]; then | |
| echo " → Copying .venv..." | |
| cp -R "$repo_root/.venv" "$worktree_path/.venv" | |
| fi | |
| if [ -d "$repo_root/node_modules" ]; then | |
| echo " → Copying node_modules..." | |
| cp -R "$repo_root/node_modules" "$worktree_path/node_modules" | |
| fi | |
| echo "" | |
| echo "✓ Worktree created successfully!" | |
| echo "✓ Starting Claude Code in $worktree_path" | |
| echo "" | |
| # Change to directory and start claude | |
| cd "$worktree_path" && claude | |
| # After claude exits, return to original directory | |
| cd "$current_dir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment