Créer une Git Worktree

VérifiéPrudence

Crée des worktrees Git dans un emplacement standardisé avec configuration DVC appropriée. Permet de travailler sur plusieurs branches simultanément.

Spar Skills Guide Bot
DeveloppementIntermédiaire
6002/06/2026
Claude Code
#git#worktree#branching#dvc#environment

Recommandé pour

Notre avis

Crée des worktrees Git dans un répertoire standardisé avec configuration DVC appropriée.

Points forts

  • Automatise la création de worktrees avec un emplacement cohérent
  • Gère la copie des fichiers .env et la configuration DVC
  • Facilite le travail simultané sur plusieurs branches

Limites

  • Nécessite que DVC soit déjà configuré dans le dépôt pour les étapes DVC
  • Ne gère pas les branches existantes avec des divergences importantes
  • Le copier-coller du .env peut ne pas être suffisant pour certains environnements complexes
Quand l'utiliser

Utilisez cette compétence lorsque vous devez travailler sur plusieurs branches Git en parallèle et que vous souhaitez une configuration reproductible incluant DVC et les variables d'environnement.

Quand l'éviter

Ne l'utilisez pas si vous préférez gérer les worktrees manuellement ou si votre projet n'utilise ni DVC ni de fichier .env.

Analyse de sécurité

Prudence
Score qualité85/100

The skill uses bash commands that are standard for git worktree creation, but the branch name, when inserted into commands, could be exploited for injection if not handled safely. Additionally, copying .env files could expose secrets if the worktree is not properly secured, though that is a user concern.

Points d'attention
  • The skill instructs to use user-provided branch names in shell commands. If the AI substitutes the branch name directly without proper quoting or sanitization, it could lead to command injection (e.g., branch name containing '; malicious command'). The commands should always use variable expansion with quotes to mitigate this risk.

Exemples

Create worktree for a feature branch
Create a git worktree for branch 'feature/new-api'
Create worktree for a new branch
Add a worktree for a new branch called 'hotfix/production-fix'
Set up worktree with DVC
Set up a git worktree for 'experiment/analysis' and configure DVC cache

name: worktree description: This skill should be used when the user asks to "create a worktree", "add a worktree", "set up a worktree", mentions "git worktree", or wants to work on multiple branches simultaneously. user-invocable: true

Create Git Worktree

This skill creates git worktrees in a standardized location with proper DVC configuration.

When to Use

Use this skill when the user:

  • Asks to create a new git worktree
  • Wants to work on multiple branches simultaneously
  • Mentions "git worktree add" or similar

Worktree Location

All worktrees are created under .worktrees/ in the repository root:

repo/
├── .worktrees/
│   ├── feature-branch-1/
│   └── feature-branch-2/
├── src/
└── ...

Instructions

1. Determine the Repository Root

Find the git repository root:

git rev-parse --show-toplevel

2. Create the Worktrees Directory

Ensure the .worktrees directory exists:

mkdir -p "$(git rev-parse --show-toplevel)/.worktrees"

3. Create the Worktree

Create the worktree with the specified branch:

REPO_ROOT="$(git rev-parse --show-toplevel)"
git worktree add "$REPO_ROOT/.worktrees/<branch-name>" <branch-name>

If creating a new branch:

git worktree add -b <new-branch-name> "$REPO_ROOT/.worktrees/<new-branch-name>"

4. Copy .env File

Copy .env from the project root to the worktree so environment variables are available:

REPO_ROOT="$(git rev-parse --show-toplevel)"
WORKTREE_PATH="$REPO_ROOT/.worktrees/<branch-name>"

# Copy .env if it exists
[ -f "$REPO_ROOT/.env" ] && cp "$REPO_ROOT/.env" "$WORKTREE_PATH/"

5. Handle DVC Configuration (if applicable)

Check if the repository uses DVC by looking for .dvc/ directory:

if [ -d "$(git rev-parse --show-toplevel)/.dvc" ]; then
    echo "DVC detected"
fi

If DVC is present and .dvc/config.local exists, copy and update it:

  1. Copy the local config:
REPO_ROOT="$(git rev-parse --show-toplevel)"
WORKTREE_PATH="$REPO_ROOT/.worktrees/<branch-name>"
cp "$REPO_ROOT/.dvc/config.local" "$WORKTREE_PATH/.dvc/config.local"
  1. Update the cache directory to point to the main repo's cache:
REPO_ROOT="$(git rev-parse --show-toplevel)"
WORKTREE_PATH="$REPO_ROOT/.worktrees/<branch-name>"
cat > "$WORKTREE_PATH/.dvc/config.local" << EOF
[cache]
    dir = $REPO_ROOT/.dvc/cache
EOF

Note: If the original .dvc/config.local contains other settings beyond cache configuration, preserve those settings and only update/add the cache dir setting.

Complete Example

Creating a worktree for branch feature/new-api:

REPO_ROOT="$(git rev-parse --show-toplevel)"
BRANCH_NAME="feature/new-api"
WORKTREE_DIR="$REPO_ROOT/.worktrees/$BRANCH_NAME"

mkdir -p "$REPO_ROOT/.worktrees"
git worktree add "$WORKTREE_DIR" "$BRANCH_NAME"

# Copy .env if it exists
[ -f "$REPO_ROOT/.env" ] && cp "$REPO_ROOT/.env" "$WORKTREE_DIR/"

if [ -f "$REPO_ROOT/.dvc/config.local" ]; then
    mkdir -p "$WORKTREE_DIR/.dvc"
    cat > "$WORKTREE_DIR/.dvc/config.local" << EOF
[cache]
    dir = $REPO_ROOT/.dvc/cache
EOF
fi

echo "Worktree created at: $WORKTREE_DIR"

Notes

  • Worktree names typically match branch names, with slashes replaced by the filesystem
  • The .worktrees/ directory should be added to .gitignore if not already
  • Use git worktree list to see all worktrees
  • Use git worktree remove <path> to remove a worktree
Skills similaires