Using git worktrees

From microsoft/FluidFramework · View interactive version

Using git worktrees is an AI agent skill from the GitHub repository microsoft/FluidFramework. Use this whenever you need to create an isolated workspace.

How to use this skill

Skill file

<required>

CRITICAL Add the following steps to your Todo list using TodoWrite:

  1. Find the worktrees directory.
  1. Verify .gitignore before creating a worktree using the Bash tool:
# Check if directory pattern in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignore
  1. Create the worktree
  1. Auto-detect and run project 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
  1. Run tests to ensure the worktree is 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.

  1. Report Location
New working directory: <full-path>
Tests passing (<N> tests, 0 failures)
All commands and tools will now refer to: <full-path>
  1. Understand that you are now in a new working directory. Your Bash tool instructions from here on out should refer to the worktree directory, NOT your original directory. This is ABSOLUTELY CRITICAL.

</required>

Maintaining Working Directory in Worktree

CRITICAL: Once you create and enter a worktree, you must stay within

it for the entire session.

Rules:

  1. Never use cd .. from within a worktree - It will eventually take

you outside the worktree boundary

  1. Always use absolute paths for commands - Use npm run lint from

within the worktree, not cd .. && npm run lint

  1. If you need to run root-level commands, use the full worktree path:

<bad-example>

cd .. && npm run lint

</bad-example>

<good-example>

npm run lint # (from worktree root)

</good-example>

<good-example>

cd /home/$USER/code/project/.worktrees/branch-name && npm run lint

</good-example>

  1. Verify your location frequently:
pwd  # Should show .worktrees/branch-name in path
git branch  # Should show * on your feature branch, not main
  1. If you accidentally exit the worktree:

Red Flags:

Quick Reference

| Situation | Action |

| --------------------------- | -------------------------- |

| .worktrees/ exists | Use it (verify .gitignore) |

| `.worktree/s does not exist | Check CLAUDE.md → Ask user |

| Directory not in .gitignore | Add it immediately |

| Tests fail during baseline | Report failures + ask |

| No package.json/Cargo.toml | Skip dependency install |

Common Mistakes

Skipping .gitignore verification

Assuming directory location

Missing project installation

Proceeding with failing tests

Hardcoding setup commands

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 myproject/.worktrees/auth
Tests passing (47 tests, 0 failures)
Ready to implement auth feature

Red Flags

Never:

Always:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.