Skip to content

Instantly share code, notes, and snippets.

@rozsival
Last active January 20, 2026 22:04
Show Gist options
  • Select an option

  • Save rozsival/b0050a9d09042e6130f4b49d0b19c2a4 to your computer and use it in GitHub Desktop.

Select an option

Save rozsival/b0050a9d09042e6130f4b49d0b19c2a4 to your computer and use it in GitHub Desktop.
Git Worktree Setup

🌳 git-worktree-setup

A utility script for cloning and setting up Git repositories as bare clones with an opinionated worktree-enabled structure.

πŸ€” Why?

Git worktrees are becoming the go-to solution for managing repositories with AI-driven development workflows, allowing developers to work on multiple features in parallel while your AI assistant works in separate branches without conflicts.

πŸ“¦ Installation

sudo mv ./git-worktree-setup.sh /usr/local/bin/git-worktree-setup
sudo chown root:wheel /usr/local/bin/git-worktree-setup
sudo chmod +x /usr/local/bin/git-worktree-setup

βœ… Now you have the git-worktree-setup command available in your shell.

πŸš€ Usage

git-worktree-setup <repository-url> <worktree-directory> [default-worktree-branch]

Example

git-worktree-setup git@github.com:username/repository.git ~/projects/repository

This will set up the repository as a bare clone with main as the initial worktree:

projects/
β”œβ”€ repository/
β”‚  β”œβ”€ .bare/          # Bare repository
β”‚  β”œβ”€ main/           # Default worktree

πŸ’‘ Workflow

Once set up, open each worktree in your IDE or Claude terminal session separately. This allows you to:

  • ✨ Work on feature A in the main worktree
  • πŸ€– Let Claude work on feature B in a separate worktree
  • 🎯 Avoid context switching and merge conflicts
  • ⚑ Switch between features instantly

πŸ› οΈ Managing Worktrees

We highly recommend Worktrunk for managing your worktrees. It's an amazing CLI tool that makes creating, switching, and deleting worktrees effortless.

Worktrunk fully supports bare repositories via configuration, making it the perfect companion to this setup script.


Happy parallel development! πŸŽ‰

#!/usr/bin/env bash
# Utility script to clone and setup a git repository for worktree usage.
# It creates a bare clone of the repository and sets up a worktree in the specified directory with `main` as the default worktree branch.
set -e
# Check if git is available
command -v git >/dev/null 2>&1 || { echo "Error: git is required but not installed"; exit 1; }
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
echo "Usage: $0 <repository-url> <worktree-directory> [default-worktree-branch]"
exit 1
fi
GIT_REPO_URL=$1
WORKTREE_DIR=$2
DEFAULT_WORKTREE_BRANCH=$3
# Default branch if not provided
if [ -z "$DEFAULT_WORKTREE_BRANCH" ]; then
DEFAULT_WORKTREE_BRANCH="main"
fi
echo "Cloning repository '$GIT_REPO_URL' into bare and setting up worktree at '$WORKTREE_DIR'"
# 1. Create the worktree directory if it doesn't exist
mkdir -p "$WORKTREE_DIR"
cd "$WORKTREE_DIR"
# Check if .bare already exists
if [ -d ".bare" ]; then
echo "Error: $WORKTREE_DIR/.bare already exists"
exit 1
fi
# 2. Clone the repository as a bare repository
git clone --bare "$GIT_REPO_URL" .bare
# 3. Ensure remotes are fetched correctly for the bare repository
cd .bare
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
# 4. Setup default worktree branch
git worktree add "../$DEFAULT_WORKTREE_BRANCH"
# 5. Set default branch tracking
cd "../$DEFAULT_WORKTREE_BRANCH"
git branch --set-upstream-to="origin/$DEFAULT_WORKTREE_BRANCH" "$DEFAULT_WORKTREE_BRANCH"
echo "Repository cloned as bare and worktree set up at '$WORKTREE_DIR'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment