Using git worktrees

From andreaswasita/copilot-agents-dojo · View interactive version

Using git worktrees is an AI agent skill from the GitHub repository andreaswasita/copilot-agents-dojo. Isolates each task in its own git worktree off main.

How to use this skill

Skill file

Using Git Worktrees Skill

Creates an isolated git worktree on a dedicated feature branch for every development task, so main is never touched until finishing-a-development-branch says so. Does NOT replace feature branches when worktrees are impractical (CI, single-file fix) — the isolation principle is what matters.

When to Use

Prerequisites

How to Run

1. From the main repo root, create the worktree with a new branch.
2. Change into the worktree directory.
3. Install dependencies inside it.
4. Confirm a green test baseline.
5. Work only inside the worktree — never touch the main worktree.

Quick Reference

| Operation | Command |

|---|---|

| Create | git worktree add ../<dirname> -b <branch> |

| List | git worktree list |

| Remove | git worktree remove ../<dirname> |

| Prune stale | git worktree prune |

| Branch prefix | Use |

|---|---|

| feature/<slug> | New features |

| fix/<slug> | Bug fixes |

| experiment/<slug> | Spikes / throwaway |

| Dependency bootstrap | Detect signal |

|---|---|

| npm install | package.json |

| pip install -r requirements.txt | requirements.txt |

| go mod download | go.mod |

| mvn dependency:resolve | pom.xml |

| dotnet restore | *.csproj |

Procedure

Step 1: Create the Worktree

From the main repo root, run via the powershell tool:

git worktree add ../project-feature-name -b feature/feature-name
cd ../project-feature-name

Use a directory name that is obviously not the main repo so you cannot edit the wrong one by accident.

Step 2: Install Dependencies

Detect the stack from the table above and run the matching install command. A worktree shares .git, not node_modules / .venv / build artifacts.

Step 3: Establish a Green Baseline

Run the project's test command via the powershell tool. Capture pass/fail counts before changing any code. If the baseline is red, fix it first or explicitly acknowledge the broken tests in tasks/todo.md.

git status --porcelain         # must be empty
git branch --show-current      # must be the new feature branch
<project test command>         # must exit 0 for a clean baseline

Step 4: Work Inside the Worktree

All commits go to the feature branch. The main repo's working tree is read-only for the duration. Open files and run commands only from inside the worktree directory.

Step 5: When Worktrees Are Impractical

For CI environments, single-file fixes, or systems without worktree support:

git checkout -b feature/<slug>

The isolation principle still applies: never commit to main.

Step 6: Cleanup (handled later)

Worktree teardown is owned by finishing-a-development-branch. Do not remove the worktree mid-task.

Pitfalls

Verification

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.