Loading...
Loading...
Worktree-native merge engineer — git worktree lifecycle, isolated merges and conflict resolution, worktree path conventions, parallel worktree operations, and cleanup automation. Invoke via /git-merge-expert-worktree or when user says "merge in worktree", "isolated merge", "worktree merge".
npx skill4agent add ulpi-io/skills git-merge-expert-worktreeworktree-<name>.claude/worktrees/<name>node_modules.envpnpm installgit worktree prunegit worktree listgit worktree removerm -rfpnpm installnode_modulesgit worktree listworktree-git tag backup/<branch>-pre-mergegit worktree pruneworktree-<name>.claude/worktrees/mainmasterdevelop--no-verifypnpm installnode_modulesgit worktree remove--force.claude/worktrees/<name>../project--purpose1. DISCOVER
- List existing worktrees: git worktree list
- Identify current worktree: git rev-parse --show-toplevel
- Check if inside a worktree or the main working tree
- Determine if a new worktree is needed or an existing one can be reused
2. CREATE
- Choose path: .claude/worktrees/<name> (default) or sibling (../project--purpose)
- Choose branch: worktree-<name> for managed sessions, or descriptive name for ad-hoc
- Create: git worktree add [-b <branch>] <path> <start-point>
- Verify: git worktree list shows the new worktree
3. SETUP
- Symlink node_modules from main repo (if configured)
- Copy .env files from main worktree
- Run postCreate commands (pnpm install, etc.)
- Verify: project builds or runs correctly in the worktree
4. WORK
- Perform the merge, conflict resolution, or other operation
- All git commands operate in the worktree context
- The main working tree remains untouched
5. MERGE (if applicable)
- Create backup tag on target branch
- Perform merge: git merge --no-ff <source>
- If conflicts, resolve using tiered approach (see Conflict Resolution)
- Commit the merge result
6. VALIDATE
- git status (clean? no unresolved conflicts?)
- Run build: pnpm build (or project-specific)
- Run tests: pnpm test (or project-specific)
- Run typecheck: pnpm typecheck (if applicable)
- git log --oneline --graph to verify branch topology
7. CLEANUP
- Push results if needed: git push origin <branch>
- Return to main worktree: cd <main-worktree-path>
- Remove worktree: git worktree remove <path>
- Delete session branch: git branch -D worktree-<name>
- Prune stale refs: git worktree prune
- Verify: git worktree list shows only expected entries
- Verify: main working tree is unchangedworktree-<name># Branch format
worktree-<name>
# Example
worktree-merge-payments
# These branches are ephemeral — created with the worktree, deleted after.claude/worktrees/<repo>/
.claude/
worktrees/
<name>/ # Worktree root (one per session)
.git # File pointing to main repo's .git/worktrees/<name>
node_modules -> ../../.. # Symlinked (if configured)
.env # Copied from main repo
src/ # Full working tree
....claude/worktrees/.gitignorepnpm installCommon symlinks:
- node_modules
- .env
- .env.localpnpm installCommon postCreate commands:
- pnpm install --frozen-lockfile
- pnpm build.gitgit worktree remove --force <path>worktree-rm -rf.git# 1. Create worktree on the TARGET branch
git worktree add ../merge-workspace main
# 2. In the worktree, merge the SOURCE branch
cd ../merge-workspace
git merge --no-ff origin/feat/my-feature
# 3. If conflicts, resolve them here (main working tree untouched)
# (use Read/Edit tools on files in ../merge-workspace/)
# 4. Validate
pnpm install && pnpm build && pnpm test
# 5. Push from the worktree
git push origin main
# 6. Clean up
cd -
git worktree remove ../merge-workspace# 1. Worktree path: .claude/worktrees/<name> (Claude Code default)
# 2. Create worktree with session branch
git worktree add -b worktree-merge-session \
.claude/worktrees/merge-session \
main
# 3. Setup: symlink node_modules, copy .env
ln -s "$(pwd)/node_modules" .claude/worktrees/merge-session/node_modules
cp .env .claude/worktrees/merge-session/.env
# 4. Merge in the worktree
cd .claude/worktrees/merge-session
git merge --no-ff origin/feat/my-feature
# 5. Validate, push, cleanup (same as standard)pnpm install.snap.d.tsgit merge --abort# 1. Abort the merge
git merge --abort
# 2. If the worktree is in a bad state, remove and recreate
cd /main/repo
git worktree remove ../merge-workspace --force
git worktree add ../merge-workspace main
# 3. Try again with a different strategy (rebase, cherry-pick, etc.)
cd ../merge-workspace
git rebase origin/feat/my-featurefatal: '<branch>' is already checked out at '<path>'-bgit worktree listgit worktree listgit worktree removegit worktree prunenode_modulespnpm buildnode_modulesln -s /main/repo/node_modules .pnpm installpnpm-lock.yamlgit checkout --theirs pnpm-lock.yaml
pnpm install
git add pnpm-lock.yamlgit worktree addmkdir -p .claude/worktrees/git fetch . <worktree-branch>:<target-branch>git pull| Command | Purpose |
|---|---|
| Create worktree from existing branch |
| Create worktree with new branch |
| Create worktree at detached HEAD |
| List all worktrees (path, HEAD, branch) |
| Machine-readable worktree list |
| Remove a worktree (must be clean) |
| Force remove worktree (even if dirty) |
| Clean up stale worktree references |
| Preview what prune would remove |
| Relocate a worktree |
| Prevent worktree from being pruned |
| Allow worktree to be pruned again |
| Fix references after manual directory moves |
| Command | Purpose |
|---|---|
| Show root of current worktree |
| Show shared .git directory |
| Check if inside any worktree |
| Show gitdir pointer to main repo |
| List all session branches |
| Command | Purpose |
|---|---|
| Install deps in worktree (lockfile unchanged) |
| Install deps and update lockfile if needed |
| Symlink node_modules from main repo |
| Copy env files into worktree |
feat/paymentsmain# 1. Discover
git worktree list
# /Users/me/myproject abc1234 [develop] ← main working tree
# 2. Create worktree on the target branch
git worktree add ../myproject--merge-payments main
# 3. Setup
cd ../myproject--merge-payments
pnpm install --frozen-lockfile
# 4. Backup + Merge
git tag backup/main-pre-merge-payments
git merge --no-ff origin/feat/payments
# 5. Validate
pnpm build && pnpm test && pnpm typecheck
# 6. Push
git push origin main
# 7. Cleanup
cd /Users/me/myproject
git worktree remove ../myproject--merge-payments
git worktree prunefeat/authdevelop# 1. Create worktree
git worktree add ../myproject--merge-auth develop
cd ../myproject--merge-auth
pnpm install
# 2. Attempt merge
git merge --no-ff origin/feat/auth
# CONFLICT in src/auth.ts and src/middleware.ts
# 3. Inspect conflicts
git diff --name-only --diff-filter=U
# → src/auth.ts, src/middleware.ts
# 4. Read both files, understand both sides
# (use Read tool on each file, find <<<<<<< markers)
# 5. Resolve each file
# (use Edit tool to replace conflict markers with correct merged code)
# 6. Regenerate lockfile if conflicted
pnpm install
git add pnpm-lock.yaml
# 7. Complete the merge
git add src/auth.ts src/middleware.ts
git commit -m "merge: feat/auth into develop — resolve auth + middleware conflicts"
# 8. Validate
pnpm build && pnpm test
# 9. Push and cleanup
git push origin develop
cd /original/repo
git worktree remove ../myproject--merge-authdevelop# 1. Discover existing worktrees
git worktree list
# 2. Create worktrees — each on a NEW branch based on develop
# (can't checkout develop in multiple worktrees)
git worktree add -b merge/feat-a ../merge-feat-a develop
git worktree add -b merge/feat-b ../merge-feat-b develop
git worktree add -b merge/feat-c ../merge-feat-c develop
# 3. Merge in each worktree (can be done in parallel)
cd ../merge-feat-a && git merge --no-ff origin/feat/feature-a
cd ../merge-feat-b && git merge --no-ff origin/feat/feature-b
cd ../merge-feat-c && git merge --no-ff origin/feat/feature-c
# 4. Validate each
cd ../merge-feat-a && pnpm install && pnpm build && pnpm test
cd ../merge-feat-b && pnpm install && pnpm build && pnpm test
cd ../merge-feat-c && pnpm install && pnpm build && pnpm test
# 5. Sequentially integrate into develop (from main repo)
cd /original/repo
git checkout develop
git merge --ff-only merge/feat-a # first one fast-forwards
git merge --no-ff merge/feat-b # may need conflict resolution
git merge --no-ff merge/feat-c # may need conflict resolution
# 6. Clean up all worktrees and branches
git worktree remove ../merge-feat-a
git worktree remove ../merge-feat-b
git worktree remove ../merge-feat-c
git branch -D merge/feat-a merge/feat-b merge/feat-c
git worktree prune# 1. Create worktree directory structure (if needed)
mkdir -p .claude/worktrees
# 2. Create worktree with session branch
git worktree add \
-b worktree-merge-sprint-42 \
.claude/worktrees/merge-sprint-42 \
develop
# 3. Setup symlinks
cd .claude/worktrees/merge-sprint-42
ln -s "$(git rev-parse --git-common-dir)/../../node_modules" ./node_modules
cp "$(git rev-parse --git-common-dir)/../../.env" ./.env
# 4. Do the work...
# 5. Cleanup
cd "$(git rev-parse --git-common-dir)/../.."
git worktree remove .claude/worktrees/merge-sprint-42 --force
# Only delete worktree- branch (safe — it's ephemeral)
git branch -D worktree-merge-sprint-42
git worktree prune# 1. List all worktrees
git worktree list
# /Users/me/myproject abc1234 [develop]
# /Users/me/myproject/.claude/worktrees/session-old def5678 [worktree-session-old] ← stale?
# 2. Check for stale references
git worktree prune --dry-run
# Removing worktrees/session-old: gitdir file points to non-existing location
# 3. Prune stale references
git worktree prune
# 4. Clean up orphaned session branches
git branch --list "worktree-*"
# worktree-session-old
# worktree-session-older
# 5. Verify each branch has no associated worktree, then delete
git worktree list # confirm no worktree uses these branches
git branch -D worktree-session-old worktree-session-older
# 6. Final verification
git worktree list # only main worktree remains
git branch --list "worktree-*" # no orphaned branches# 1. Situation: merge in ../merge-workspace produced bad results
cd ../merge-workspace
git status
# On branch main, merge in progress, 3 conflicts unresolved
# 2. Abort the merge
git merge --abort
# 3. If still in bad state, remove and recreate the worktree
cd /original/repo
git worktree remove ../merge-workspace --force
git worktree add ../merge-workspace main
# 4. Retry with a different strategy
cd ../merge-workspace
pnpm install
# Option A: Try rebase instead
git rebase origin/feat/my-feature
# Option B: Try cherry-pick specific commits
git log --oneline main..origin/feat/my-feature
git cherry-pick abc1234 def5678
# 5. Validate
pnpm build && pnpm test
# 6. Push and cleanup
git push origin main
cd /original/repo
git worktree remove ../merge-workspace
git worktree prune# 1. Create worktree on the PR branch
git fetch origin pull/77/head:pr-77
git worktree add ../review-pr77 pr-77
# 2. Setup and inspect
cd ../review-pr77
pnpm install
git log --oneline main..HEAD
git diff --stat main...HEAD
# 3. Validate in isolation
pnpm build && pnpm test && pnpm typecheck
# 4. If good, merge via gh (from any directory — uses repo context)
gh pr merge 77 --squash
# 5. Cleanup
cd /original/repo
git worktree remove ../review-pr77
git branch -D pr-77
git worktree prune