Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created July 13, 2025 12:58
Show Gist options
  • Save andreagrandi/542b438bf0017d93aff2b640037e3ce1 to your computer and use it in GitHub Desktop.
Save andreagrandi/542b438bf0017d93aff2b640037e3ce1 to your computer and use it in GitHub Desktop.
Script to create a git worktree and link existing virtualenv and .envrc files
#!/bin/bash
set -e
# Usage: ./git-add-worktree.sh <base-branch> <new-branch-name>
BASE_BRANCH="$1"
NEW_BRANCH="$2"
WORKTREE_PATH="../my-project-worktrees/$NEW_BRANCH"
# Create the worktree and new branch from base
git worktree add -b "$NEW_BRANCH" "$WORKTREE_PATH" "$BASE_BRANCH"
# Set up symlinks from the new worktree folder
cd "$WORKTREE_PATH"
ln -s ../../my-project/.venv .venv
ln -s ../../my-project/.envrc .envrc
direnv allow
echo "✅ Worktree for '$NEW_BRANCH' based on '$BASE_BRANCH' set up at '$WORKTREE_PATH'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment