# /sync — Merge Worktree Branches to Main

Safely merge worktree branches back to the main branch.

## Usage

```
/sync                      # Interactive — show branches, user picks one
/sync <branch>             # Merge specific branch to main
/sync rebase <branch>      # Rebase onto main first, then merge
/sync all                  # Merge all worktree branches sequentially
```

**Examples:**
- `/sync`
- `/sync feature/auth-oauth`
- `/sync rebase feature/billing-v2`
- `/sync all`

## Workflow

### Interactive Mode (no args)
1. List all worktree branches:
   ```bash
   git worktree list
   git branch --list 'feature/*' --list 'fix/*'
   ```
2. Show status of each branch (ahead/behind main):
   ```bash
   git log main..<branch> --oneline
   ```
3. Present list to user and ask which to merge
4. Proceed with the selected branch

### Single Branch Merge
1. Verify the branch exists and has commits ahead of main:
   ```bash
   git log main..<branch> --oneline
   ```
2. Switch to main:
   ```bash
   git checkout main
   git pull origin main
   ```
3. Merge the branch:
   ```bash
   git merge <branch> --no-ff -m "merge: <branch> into main"
   ```
4. If merge conflicts:
   - Show the conflicts to the user
   - Ask for guidance on resolution
   - NEVER force-resolve without user input
5. Run verification after merge:
   ```bash
   npx tsc --noEmit
   npx next lint
   ```
6. Push main:
   ```bash
   git push origin main
   ```
7. Clean up: offer to delete the merged branch and worktree

### Rebase Mode
1. Checkout the feature branch
2. Rebase onto main:
   ```bash
   git rebase main
   ```
3. If conflicts during rebase, show to user and ask for guidance
4. After clean rebase, proceed with merge as above

### All Mode
1. List all worktree branches
2. Sort by dependency order if possible
3. Merge each one sequentially (not parallel — order matters)
4. Stop immediately if any merge has conflicts
5. Report results for each branch

## Key Rules
- NEVER force push to main
- NEVER auto-resolve merge conflicts
- Always pull latest main before merging
- Always run verification after merge
- Always offer to clean up merged branches/worktrees
