Git Worktree
Create a dedicated git worktree for the current task, then keep working inside that new directory instead of the original checkout.
This skill is about execution, not just advice. When it triggers, actually create the worktree unless the repository state makes that impossible.
Do not introduce helper scripts for this skill. Use direct git and shell commands inline.
When To Use
- The user explicitly asks for a new
git worktree, independent branch directory, or isolated workspace
- The current checkout contains unrelated local changes and isolation is the safest way forward
- The user wants parallel work on multiple tasks without stashing or disturbing the original worktree
- The user says things like "create a separate branch folder", "open a fresh worktree", "use a clean checkout", or "work in an isolated workspace"
Core Rule
After creating the worktree, treat the new path as the active working directory for the rest of the task.
In any agent environment, "enter the directory" means:
- Run subsequent commands against the new worktree path
- Apply all edits under that worktree path
- Do not keep using the original checkout by accident
- Confirm the handoff by running at least one follow-up command in the new worktree
Never claim you "switched" unless your subsequent actions actually target the new worktree_path.
If your environment supports a per-command working directory, use it for every later command. If it does not, prefix later commands with an explicit cd <worktree_path> && ....
Name Derivation
- Derive a short ASCII kebab-case task slug from the user's real task, such as
login-timeout-fix or user-export
- Do not use generic names like
git-worktree, new-worktree, or task unless the request is too vague
- If the request is mostly non-ASCII or no good slug is obvious, fall back to
task-$(date +%Y%m%d-%H%M%S)
- Default branch prefix is
worktree/
- Default worktree directory is a sibling of the repository root, named
<repo-name>-<slug>
Default Workflow
Inspect the repository context from the current checkout:
git rev-parse --show-toplevel
git branch --show-current
git status --short
git worktree list --porcelain
Decide a task slug yourself using the rules above
Build branch and path names inline, then create the worktree with direct shell commands like:
repo_root=$(git rev-parse --show-toplevel)
repo_name=$(basename "$repo_root")
parent_dir=$(dirname "$repo_root")
source_branch=$(git -C "$repo_root" branch --show-current)
if [ -n "$source_branch" ]; then
source_ref="$source_branch"
else
source_ref="HEAD@$(git -C "$repo_root" rev-parse --short HEAD)"
fi
slug="<task-slug>"
base_branch="worktree/$slug"
branch_name="$base_branch"
base_path="$parent_dir/$repo_name-$slug"
worktree_path="$base_path"
index=2
while git -C "$repo_root" show-ref --verify --quiet "refs/heads/$branch_name" || [ -e "$worktree_path" ]; do
branch_name="${base_branch}-$index"
worktree_path="${base_path}-$index"
index=$((index + 1))
done
git -C "$repo_root" worktree add -b "$branch_name" "$worktree_path" HEAD
Immediately verify the handoff inside the new worktree, for example:
pwd
git status --short --branch
These verification commands must run against worktree_path.
Announce the new active path briefly, then continue the main task there
For the remainder of the task, use worktree_path as the working directory for every relevant command or edit operation
Behavior Rules
- Default base ref is
HEAD from the current checkout so uncommitted local changes are not dragged into the new worktree
- If a branch name or path already exists, auto-increment it instead of failing
- If you are already inside a non-default worktree and the user still wants another isolated workspace, create a new one from the current
HEAD
- If the directory is not a Git repository, explain that clearly and do not pretend a worktree was created
- If worktree creation succeeds, continue the user's actual task instead of stopping at setup
- If worktree creation fails because of filesystem permissions, request the minimal approval needed and retry
Uncommitted Change Policy
The safe default is isolation from uncommitted changes.
- If the source checkout is dirty, still create the new worktree from
HEAD unless the user explicitly asks to carry local edits over
- Do not silently stash, reset, or move the user's existing changes
- If the user wants local edits copied into the new worktree, use an explicit flow such as a temporary commit, patch, or cherry-pick, and say what you are doing
Output Contract
When you use this skill:
- Tell the user which branch and directory were created
- Make it clear that subsequent work is now happening inside that path
- Mention the source ref and whether the original checkout was dirty when that context matters
- Do not stop after setup if the user asked for additional work; continue the task in the new worktree
Example
User request:
Create a separate worktree for this task and then start implementing it.
Expected behavior:
- Inspect current repo status
- Create a new
worktree/... branch and sibling directory with direct git worktree commands
- Switch all following commands to that directory
- Continue the requested implementation there
Cleanup
Only remove a worktree when the user asks or when cleanup is clearly part of the task.
Before cleanup:
- Check status in the worktree you created
- Make sure you are removing the correct path
- Never remove the user's original checkout
1---2name: git-worktree3description: Create and actively use an isolated git worktree for the user's task, then continue the task inside that new directory. Use this whenever the user asks for a separate worktree, isolated checkout, clean branch directory, safer parallel changes, or a fresh workspace to avoid unrelated local edits.4---56# Git Worktree78Create a dedicated `git worktree` for the current task, then keep working inside that new directory instead of the original checkout.910This skill is about execution, not just advice. When it triggers, actually create the worktree unless the repository state makes that impossible.1112Do not introduce helper scripts for this skill. Use direct `git` and shell commands inline.1314## When To Use1516- The user explicitly asks for a new `git worktree`, independent branch directory, or isolated workspace17- The current checkout contains unrelated local changes and isolation is the safest way forward18- The user wants parallel work on multiple tasks without stashing or disturbing the original worktree19- The user says things like "create a separate branch folder", "open a fresh worktree", "use a clean checkout", or "work in an isolated workspace"2021## Core Rule2223After creating the worktree, treat the new path as the active working directory for the rest of the task.2425In any agent environment, "enter the directory" means:2627- Run subsequent commands against the new worktree path28- Apply all edits under that worktree path29- Do not keep using the original checkout by accident30- Confirm the handoff by running at least one follow-up command in the new worktree3132Never claim you "switched" unless your subsequent actions actually target the new `worktree_path`.3334If your environment supports a per-command working directory, use it for every later command. If it does not, prefix later commands with an explicit `cd <worktree_path> && ...`.3536## Name Derivation3738- Derive a short ASCII kebab-case task slug from the user's real task, such as `login-timeout-fix` or `user-export`39- Do not use generic names like `git-worktree`, `new-worktree`, or `task` unless the request is too vague40- If the request is mostly non-ASCII or no good slug is obvious, fall back to `task-$(date +%Y%m%d-%H%M%S)`41- Default branch prefix is `worktree/`42- Default worktree directory is a sibling of the repository root, named `<repo-name>-<slug>`4344## Default Workflow45461. Inspect the repository context from the current checkout:47 - `git rev-parse --show-toplevel`48 - `git branch --show-current`49 - `git status --short`50 - `git worktree list --porcelain`512. Decide a task slug yourself using the rules above523. Build branch and path names inline, then create the worktree with direct shell commands like:5354 ```bash55 repo_root=$(git rev-parse --show-toplevel)56 repo_name=$(basename "$repo_root")57 parent_dir=$(dirname "$repo_root")58 source_branch=$(git -C "$repo_root" branch --show-current)59 if [ -n "$source_branch" ]; then60 source_ref="$source_branch"61 else62 source_ref="HEAD@$(git -C "$repo_root" rev-parse --short HEAD)"63 fi6465 slug="<task-slug>"66 base_branch="worktree/$slug"67 branch_name="$base_branch"68 base_path="$parent_dir/$repo_name-$slug"69 worktree_path="$base_path"70 index=27172 while git -C "$repo_root" show-ref --verify --quiet "refs/heads/$branch_name" || [ -e "$worktree_path" ]; do73 branch_name="${base_branch}-$index"74 worktree_path="${base_path}-$index"75 index=$((index + 1))76 done7778 git -C "$repo_root" worktree add -b "$branch_name" "$worktree_path" HEAD79 ```80814. Immediately verify the handoff inside the new worktree, for example:8283 ```bash84 pwd85 git status --short --branch86 ```8788 These verification commands must run against `worktree_path`.89905. Announce the new active path briefly, then continue the main task there916. For the remainder of the task, use `worktree_path` as the working directory for every relevant command or edit operation9293## Behavior Rules9495- Default base ref is `HEAD` from the current checkout so uncommitted local changes are not dragged into the new worktree96- If a branch name or path already exists, auto-increment it instead of failing97- If you are already inside a non-default worktree and the user still wants another isolated workspace, create a new one from the current `HEAD`98- If the directory is not a Git repository, explain that clearly and do not pretend a worktree was created99- If worktree creation succeeds, continue the user's actual task instead of stopping at setup100- If worktree creation fails because of filesystem permissions, request the minimal approval needed and retry101102## Uncommitted Change Policy103104The safe default is isolation from uncommitted changes.105106- If the source checkout is dirty, still create the new worktree from `HEAD` unless the user explicitly asks to carry local edits over107- Do not silently stash, reset, or move the user's existing changes108- If the user wants local edits copied into the new worktree, use an explicit flow such as a temporary commit, patch, or cherry-pick, and say what you are doing109110## Output Contract111112When you use this skill:113114- Tell the user which branch and directory were created115- Make it clear that subsequent work is now happening inside that path116- Mention the source ref and whether the original checkout was dirty when that context matters117- Do not stop after setup if the user asked for additional work; continue the task in the new worktree118119## Example120121User request:122123```text124Create a separate worktree for this task and then start implementing it.125```126127Expected behavior:128129- Inspect current repo status130- Create a new `worktree/...` branch and sibling directory with direct `git worktree` commands131- Switch all following commands to that directory132- Continue the requested implementation there133134## Cleanup135136Only remove a worktree when the user asks or when cleanup is clearly part of the task.137138Before cleanup:139140- Check status in the worktree you created141- Make sure you are removing the correct path142- Never remove the user's original checkout