Worktree

From huzaifa525/claude-code-optimizer · View interactive version

Worktree is an AI agent skill from the GitHub repository huzaifa525/claude-code-optimizer. Use when the user wants to work in an isolated branch, start a safe development environment, or avoid polluting the main branch.

How to use this skill

Skill file

Create an isolated git worktree for safe development on: $ARGUMENTS

Iron Law

All new feature work happens in a worktree. The main branch stays clean.

If you think "this change is small enough to do on main" — you are wrong. Worktrees cost 5 seconds. Debugging a polluted main branch costs hours.

Steps

1. Verify Clean Baseline

Before creating the worktree:

# Ensure we're in a git repo
git rev-parse --is-inside-work-tree

# Check for uncommitted changes
git status --porcelain

If there are uncommitted changes, ask the user whether to stash or commit them first. Do NOT proceed with dirty state.

2. Create the Worktree

Sanitize the branch name from the user's description:

# Create worktree on a new branch
git worktree add "../$(basename $PWD)-feature-$BRANCH_NAME" -b "feature/$BRANCH_NAME"

3. Verify Test Baseline

Run the project's test suite in the new worktree BEFORE making any changes:

cd "../$(basename $PWD)-$BRANCH_NAME"
# Run tests (detect test command from package.json, Makefile, etc.)

If tests fail BEFORE any changes, the baseline is broken. Stop and report.

4. Work in the Worktree

5. Finish

When work is complete, present options:

| Option | Command | When |

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

| Merge | git checkout main && git merge $BRANCH_NAME | Feature is complete and tested |

| PR | gh pr create from the worktree branch | Needs review before merge |

| Keep | Leave worktree as-is | Work in progress, will continue later |

| Discard | git worktree remove ../worktree-dir | Experiment failed, throw it away |

Red Flags

<!-- Skill by Huzefa Nalkheda Wala | github.com/huzaifa525 | claude-code-optimizer -->

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.