Created
January 3, 2026 02:58
-
-
Save pirosikick/bbc1b10b0c4b9a2b149609a50d8e32dd to your computer and use it in GitHub Desktop.
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/bash | |
| set -e | |
| if [ $# -ne 1 ]; then | |
| echo "Usage: create-git-worktree <worktree-name>" >&2 | |
| exit 1 | |
| fi | |
| WORKTREE_NAME="$1" | |
| if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
| echo "Error: Not inside a git repository" >&2 | |
| exit 1 | |
| fi | |
| REPO_ROOT=$(git rev-parse --show-toplevel) | |
| REPO_NAME=$(basename "$REPO_ROOT") | |
| REPO_PARENT=$(dirname "$REPO_ROOT") | |
| echo "Getting default branch..." | |
| DEFAULT_BRANCH=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|^origin/||') | |
| if [ -z "$DEFAULT_BRANCH" ]; then | |
| echo "Error: Could not determine default branch" >&2 | |
| echo "Please run: git remote set-head origin --auto" >&2 | |
| exit 1 | |
| fi | |
| echo "Default branch: $DEFAULT_BRANCH" | |
| echo "Fetching latest default branch..." | |
| if ! git fetch origin "$DEFAULT_BRANCH"; then | |
| echo "Error: Failed to fetch default branch" >&2 | |
| exit 1 | |
| fi | |
| WORKTREE_PATH="$REPO_PARENT/$REPO_NAME-$WORKTREE_NAME" | |
| echo "Creating worktree: $WORKTREE_PATH" | |
| if ! git worktree add -b "$WORKTREE_NAME" "$WORKTREE_PATH" "origin/$DEFAULT_BRANCH"; then | |
| echo "Error: Failed to create worktree" >&2 | |
| exit 1 | |
| fi | |
| echo "✓ Worktree created: $WORKTREE_PATH" | |
| echo "✓ Branch: $WORKTREE_NAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment