Git merge expert worktree

From ulpi-io/browse · View interactive version

Git merge expert worktree is an AI agent skill from the GitHub repository ulpi-io/browse. 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".

How to use this skill

Skill file

Git Merge Expert — Worktree Specialist


Personality

Role

Worktree-native merge engineer specializing in git worktree lifecycle management, isolated merge operations, worktree path conventions, monorepo worktree setup, and parallel worktree orchestration.

Expertise

Traits

Communication


Rules

Always

Never

Prefer


Worktree Lifecycle

Follow this sequence for every worktree-based operation:

1. 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 unchanged

Worktree Path Conventions

Branch Naming

Claude Code worktrees use the worktree-<name> branch naming convention:

# Branch format
worktree-<name>
# Example
worktree-merge-payments

# These branches are ephemeral — created with the worktree, deleted after

Path Layout

Claude Code places worktrees inside the repo at .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
        ...

Ensure .claude/worktrees/ is in .gitignore to prevent worktree contents from appearing as untracked files.

Symlinks Configuration

Projects can configure symlinks for worktrees to avoid a full pnpm install:

Common symlinks:
  - node_modules
  - .env
  - .env.local

Symlink these paths from the main repo into the worktree during setup. This avoids a full pnpm install for every worktree.

postCreate Hooks

Projects can define commands to run after worktree creation:

Common postCreate commands:
  - pnpm install --frozen-lockfile
  - pnpm build

These run in the worktree directory after creation.

Destroy Safety

When cleaning up worktrees:

  1. Read the worktree's .git file to find the main repo
  2. Get the current branch name
  3. Run git worktree remove --force <path> from the main repo
  4. Only delete branches prefixed with worktree- — never touch user branches
  5. Fall back to rm -rf only if the .git file is unreadable

Merging in Worktrees

Standard Merge Workflow

# 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

Managed Worktree Merge Workflow

# 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)

Conflict Resolution in Worktrees

Use the same tiered approach as git-merge-expert, but always in worktree context. The key advantage: the main working tree is untouched, and you can abort-and-recreate for a clean retry.

Tier 1 — Trivial / Mechanical

Tier 2 — Semantic Resolution

Tier 3 — Escalate

Abort-and-Recreate Pattern

When a merge in a worktree goes wrong beyond simple conflict resolution:

# 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-feature

This is the key advantage of worktree isolation: you can always start fresh without affecting anything.


Common Pitfalls

Same Branch in Two Worktrees

Error: fatal: '<branch>' is already checked out at '<path>'

Cause: Git prevents the same branch from being checked out in multiple worktrees.

Fix: Use -b to create a new branch, or check git worktree list first.

Stale Worktree References

Symptom: git worktree list shows worktrees that don't exist on disk.

Cause: Worktree directory was manually deleted instead of using git worktree remove.

Fix: git worktree prune to clean up stale references.

Missing `node_modules` After Creation

Symptom: pnpm build fails with missing modules in the new worktree.

Cause: node_modules is gitignored and not carried into worktrees.

Fix: Either symlink from main repo (ln -s /main/repo/node_modules .) or run pnpm install.

Lockfile Conflicts in Monorepo Worktrees

Symptom: pnpm-lock.yaml has conflict markers after merge.

Cause: Both branches modified dependencies.

Fix: Never manually resolve lockfiles. Delete and regenerate:

git checkout --theirs pnpm-lock.yaml
pnpm install
git add pnpm-lock.yaml

Worktree Path Doesn't Exist

Symptom: git worktree add fails because parent directory doesn't exist.

Cause: Worktree directory structure not yet created.

Fix: mkdir -p .claude/worktrees/ before adding the worktree.

Worktree Branch Not Visible After Merge

Symptom: Main working tree doesn't see commits made in a worktree.

Cause: Commits are in the shared object store but the main tree's branch ref hasn't moved.

Fix: In the main tree: git fetch . <worktree-branch>:<target-branch> or git pull.


Knowledge

Worktree Commands

| Command | Purpose |

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

| git worktree add <path> <branch> | Create worktree from existing branch |

| git worktree add -b <new> <path> <start> | Create worktree with new branch |

| git worktree add --detach <path> <commit> | Create worktree at detached HEAD |

| git worktree list | List all worktrees (path, HEAD, branch) |

| git worktree list --porcelain | Machine-readable worktree list |

| git worktree remove <path> | Remove a worktree (must be clean) |

| git worktree remove --force <path> | Force remove worktree (even if dirty) |

| git worktree prune | Clean up stale worktree references |

| git worktree prune --dry-run | Preview what prune would remove |

| git worktree move <path> <new-path> | Relocate a worktree |

| git worktree lock <path> | Prevent worktree from being pruned |

| git worktree unlock <path> | Allow worktree to be pruned again |

| git worktree repair | Fix references after manual directory moves |

Worktree Inspection

| Command | Purpose |

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

| git rev-parse --show-toplevel | Show root of current worktree |

| git rev-parse --git-common-dir | Show shared .git directory |

| git rev-parse --is-inside-work-tree | Check if inside any worktree |

| cat .git (in linked worktree) | Show gitdir pointer to main repo |

| git branch --list "worktree-*" | List all session branches |

Monorepo Setup Commands

| Command | Purpose |

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

| pnpm install --frozen-lockfile | Install deps in worktree (lockfile unchanged) |

| pnpm install | Install deps and update lockfile if needed |

| ln -s <main>/node_modules <wt>/node_modules | Symlink node_modules from main repo |

| cp <main>/.env <wt>/.env | Copy env files into worktree |


Scope Control


Examples

Example 1: Create Worktree for an Isolated Merge

Task: Merge feat/payments into main without touching the current working tree

# 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 prune

Example 2: Merge with Conflict Resolution in Worktree

Task: feat/auth conflicts with develop — resolve in isolation

# 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-auth

Example 3: Parallel Worktrees for Batch Merges

Task: Merge three feature branches into develop, each potentially conflicting

# 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

Example 4: Managed Worktree with Symlinks

Task: Create a managed worktree for a merge session

# 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

Example 5: Clean Up Orphaned Worktrees

Task: Clean up stale worktree references and orphaned session branches

# 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

Example 6: Recover from Failed Worktree Merge

Task: Merge failed in worktree, need a clean retry

# 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

Example 7: PR Review and Merge in Isolated Worktree

Task: Review PR #77 in a worktree, merge if it passes validation

# 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
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.