Using git worktrees

From ahmadulhoq/agentskel · View interactive version

Using git worktrees is an AI agent skill from the GitHub repository ahmadulhoq/agentskel. When running long feature implementations, parallel branch work, or build/test scripts that must not interfere with the user's active working directory. Creates isolated execution environments via git worktrees in sibling directories.

How to use this skill

Skill file

Using Git Worktrees Skill

Purpose: Keep the active repo clean. Work happens in a sibling directory — no nested git repos, no IDE re-indexing loops.

When to Use

| Situation | Use worktree? |

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

| Long feature run (> ~30 min) | Yes |

| Parallel branch comparison | Yes |

| Running full build/test suite against a branch | Yes |

| Quick hotfix or single-file change | No — use normal branch |

Setup

  1. Confirm the feature branch exists (created by git-flow before any code):

```bash

BRANCH=<branch-name>

REPO=$(basename $(git rev-parse --show-toplevel))

WORKTREE_PATH="../${REPO}-${BRANCH}"

```

  1. Create the worktree:

```bash

git worktree add "$WORKTREE_PATH" "$BRANCH"

```

  1. Enter the worktree before running any build or test commands:

```bash

cd "$WORKTREE_PATH"

```

  1. All implementation, build, and test commands execute inside $WORKTREE_PATH.

The main repo directory is effectively read-only during this session.

Rules

Cleanup (after PR is merged)

  1. Return to main repo directory, then:

```bash

git worktree remove "$WORKTREE_PATH"

git branch -d "$BRANCH" # only after merge is confirmed

```


Gate: Do not run build or test scripts in the main repo directory when a worktree

exists for the active branch.

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.