Get the FREE Ultimate OpenClaw Setup Guide →

using-git-worktrees

npx machina-cli add skill CodingCossack/agent-skills-library/using-git-worktrees --openclaw
Files (1)
SKILL.md
5.4 KB

Using Git Worktrees

Overview

Git worktrees create isolated workspaces sharing the same repository, allowing work on multiple branches simultaneously without switching.

Core principle: Systematic directory selection + safety verification = reliable isolation.

Announce at start: "I'm using the using-git-worktrees skill to set up an isolated workspace."

Directory Selection Process

Follow this priority order:

1. Check Existing Directories

# Check in priority order
ls -d .worktrees 2>/dev/null     # Preferred (hidden)
ls -d worktrees 2>/dev/null      # Alternative

If found: Use that directory. If both exist, .worktrees wins.

2. Check CLAUDE.md

grep -i "worktree.*director" CLAUDE.md 2>/dev/null

If preference specified: Use it without asking.

3. Ask User

If no directory exists and no CLAUDE.md preference:

No worktree directory found. Where should I create worktrees?

1. .worktrees/ (project-local, hidden)
2. ~/.config/superpowers/worktrees/<project-name>/ (global location)

Which would you prefer?

Safety Verification

For Project-Local Directories (.worktrees or worktrees)

MUST verify .gitignore before creating worktree:

# Check if directory pattern in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignore

If NOT in .gitignore:

Per Jesse's rule "Fix broken things immediately":

  1. Add appropriate line to .gitignore
  2. Commit the change
  3. Proceed with worktree creation

Why critical: Prevents accidentally committing worktree contents to repository.

For Global Directory (~/.config/superpowers/worktrees)

No .gitignore verification needed - outside project entirely.

Creation Steps

1. Detect Project Name

project=$(basename "$(git rev-parse --show-toplevel)")

2. Create Worktree

# Determine full path
case $LOCATION in
  .worktrees|worktrees)
    path="$LOCATION/$BRANCH_NAME"
    ;;
  ~/.config/superpowers/worktrees/*)
    path="~/.config/superpowers/worktrees/$project/$BRANCH_NAME"
    ;;
esac

# Create worktree with new branch
git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"

3. Run Project Setup

Auto-detect and run appropriate setup:

# Node.js
if [ -f package.json ]; then npm install; fi

# Rust
if [ -f Cargo.toml ]; then cargo build; fi

# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi

# Go
if [ -f go.mod ]; then go mod download; fi

4. Verify Clean Baseline

Run tests to ensure worktree starts clean:

# Examples - use project-appropriate command
npm test
cargo test
pytest
go test ./...

If tests fail: Report failures, ask whether to proceed or investigate.

If tests pass: Report ready.

5. Report Location

Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>

Quick Reference

SituationAction
.worktrees/ existsUse it (verify .gitignore)
worktrees/ existsUse it (verify .gitignore)
Both existUse .worktrees/
Neither existsCheck CLAUDE.md → Ask user
Directory not in .gitignoreAdd it immediately + commit
Tests fail during baselineReport failures + ask
No package.json/Cargo.tomlSkip dependency install

Common Mistakes

Skipping .gitignore verification

  • Problem: Worktree contents get tracked, pollute git status
  • Fix: Always grep .gitignore before creating project-local worktree

Assuming directory location

  • Problem: Creates inconsistency, violates project conventions
  • Fix: Follow priority: existing > CLAUDE.md > ask

Proceeding with failing tests

  • Problem: Can't distinguish new bugs from pre-existing issues
  • Fix: Report failures, get explicit permission to proceed

Hardcoding setup commands

  • Problem: Breaks on projects using different tools
  • Fix: Auto-detect from project files (package.json, etc.)

Example Workflow

You: I'm using the using-git-worktrees skill to set up an isolated workspace.

[Check .worktrees/ - exists]
[Verify .gitignore - contains .worktrees/]
[Create worktree: git worktree add .worktrees/auth -b feature/auth]
[Run npm install]
[Run npm test - 47 passing]

Worktree ready at /Users/jesse/myproject/.worktrees/auth
Tests passing (47 tests, 0 failures)
Ready to implement auth feature

Red Flags

Never:

  • Create worktree without .gitignore verification (project-local)
  • Skip baseline test verification
  • Proceed with failing tests without asking
  • Assume directory location when ambiguous
  • Skip CLAUDE.md check

Always:

  • Follow directory priority: existing > CLAUDE.md > ask
  • Verify .gitignore for project-local
  • Auto-detect and run project setup
  • Verify clean test baseline

Integration

Called by:

  • brainstorming Skillc(Phase 4) - REQUIRED when design is approved and implementation follows
  • Any skill needing isolated workspace

Pairs with:

  • finishing-a-development-branch Skill - REQUIRED for cleanup after work complete
  • executing-plans or subagent-driven-development Skill - Work happens in this worktree

Source

git clone https://github.com/CodingCossack/agent-skills-library/blob/main/skills/using-git-worktrees/SKILL.mdView on GitHub

Overview

Git worktrees create isolated work spaces sharing the same repository, enabling parallel work on multiple branches without switching the main working tree. This skill emphasizes safe directory selection, pre-checks like .gitignore, and a defined flow to create, configure, and verify each new worktree.

How This Skill Works

It follows a priority directory selection: prefer .worktrees, then worktrees, then CLAUDE.md preferences, and finally ask the user. Once a directory is chosen, it creates a new worktree with git worktree add -b <BRANCH> and moves into that path, then runs project setup commands based on detected artifacts (package.json for Node, Cargo.toml for Rust, requirements.txt/pyproject.toml for Python, go.mod for Go), and finally runs baseline tests to verify a clean state.

When to Use It

  • You need to implement a feature on a new branch without touching the current working tree.
  • You want to run experiments or refactors in parallel without risking the main repo.
  • Working across multiple toolchains in a single repository (e.g., Node, Rust, Python, Go).
  • Onboarding a new contributor who should work in an isolated environment.
  • Testing a breaking change in a separate environment before merging.

Quick Start

  1. Step 1: Decide to use this skill and determine the target branch and worktree location using the priority: .worktrees, worktrees, CLAUDE.md, then user prompt.
  2. Step 2: Create the new worktree with git worktree add -b <BRANCH_NAME> <PATH> and cd into the new path.
  3. Step 3: Run project setup checks (e.g., npm install, cargo build, pip install -r requirements.txt) and then run the appropriate tests (npm test, cargo test, pytest, etc.).

Best Practices

  • Prefer a project-local worktree directory (.worktrees) when possible.
  • Always verify that the worktree path is listed in .gitignore; if not, add and commit before proceeding.
  • Check CLAUDE.md for a pre-defined worktree directory preference before prompting.
  • Run the project setup (dependencies/install) and then run baseline tests in the new worktree.
  • Document the worktree location and status after creation for future reference.

Example Use Cases

  • Create a feature branch worktree for a Node.js change without affecting the main working tree.
  • Open a Rust refactor in a separate worktree while keeping the master branch untouched.
  • Spin up a Python experiment in a global worktree location for cross-project tasks.
  • Set up a Go module change in a project-local worktree and run go test.
  • Onboard a new contributor by giving them an isolated worktree environment tied to the project.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers