Created
April 13, 2026 08:38
-
-
Save fadhlirahim/c7e902a6f14e42bea8c40c25798ca62e to your computer and use it in GitHub Desktop.
Create a git worktree shortcut, copies .env, copies claude settings.local.json.example
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
| # Create a git worktree as a sibling directory for parallel Claude Code sessions. | |
| # Usage: worktree <branch-name> [base-branch] | |
| # Examples: | |
| # worktree feat/login # creates worktree from current branch | |
| # worktree fix/bug-123 main # creates worktree branching off main | |
| worktree() { | |
| local branch="$1" | |
| local base="$2" | |
| if [[ -z "$branch" ]]; then | |
| echo "Usage: worktree <branch-name> [base-branch]" | |
| echo " worktree --list" | |
| echo " worktree --rm <path>" | |
| return 1 | |
| fi | |
| if [[ "$branch" == "--list" ]]; then | |
| git worktree list | |
| return 0 | |
| fi | |
| if [[ "$branch" == "--rm" ]]; then | |
| if [[ -z "$base" ]]; then | |
| echo "Usage: worktree --rm <path>" | |
| return 1 | |
| fi | |
| git worktree remove "$base" | |
| return $? | |
| fi | |
| local repo_root | |
| repo_root=$(git rev-parse --show-toplevel 2>/dev/null) | |
| if [[ -z "$repo_root" ]]; then | |
| echo "Error: not inside a git repository." | |
| return 1 | |
| fi | |
| local repo_name | |
| repo_name=$(basename "$repo_root") | |
| # Turn branch name into a safe directory suffix: feat/login -> feat-login | |
| local dir_suffix | |
| dir_suffix=$(echo "$branch" | tr '/' '-') | |
| local worktree_path | |
| worktree_path="$(dirname "$repo_root")/${repo_name}-${dir_suffix}" | |
| if [[ -d "$worktree_path" ]]; then | |
| echo "Error: directory already exists: $worktree_path" | |
| return 1 | |
| fi | |
| # Check if branch already exists | |
| if git show-ref --verify --quiet "refs/heads/$branch"; then | |
| git worktree add "$worktree_path" "$branch" | |
| else | |
| # Create new branch, optionally from a base | |
| if [[ -n "$base" ]]; then | |
| git worktree add -b "$branch" "$worktree_path" "$base" | |
| else | |
| git worktree add -b "$branch" "$worktree_path" | |
| fi | |
| fi | |
| if [[ $? -ne 0 ]]; then | |
| return 1 | |
| fi | |
| # Copy .env if it exists | |
| if [[ -f "$repo_root/.env" ]]; then | |
| cp "$repo_root/.env" "$worktree_path/.env" | |
| echo "Copied .env to worktree." | |
| fi | |
| # Copy Claude Code local settings if example exists | |
| if [[ -f "$repo_root/.claude/settings.local.json.example" ]]; then | |
| mkdir -p "$worktree_path/.claude" | |
| cp "$repo_root/.claude/settings.local.json.example" "$worktree_path/.claude/settings.local.json" | |
| echo "Copied .claude/settings.local.json from example." | |
| fi | |
| echo "" | |
| echo "Worktree ready: $worktree_path" | |
| echo "Run: cd $worktree_path && mise trust && pnpm install" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment