worktree-workflows.mdx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. ---
  2. title: "Worktree Workflows"
  3. description: "Use Git worktrees with Cline CLI to run parallel tasks, test different approaches, and pipe context between isolated environments"
  4. ---
  5. Git worktrees let you have multiple branches checked out simultaneously in different folders. Combined with Cline CLI's `--cwd` flag, this enables powerful parallel development workflows and isolated experimentation.
  6. <Tip>
  7. New to Git worktrees? See our comprehensive [Worktrees guide](/features/worktrees) for the full concept explanation, VS Code integration, and best practices.
  8. </Tip>
  9. ## Quick Worktree Setup
  10. If you haven't used Git worktrees before, here's the essentials:
  11. ```bash
  12. # Create a new worktree in ~/worktree-a on branch feature-a
  13. git worktree add ~/worktree-a -b feature-a
  14. # Create another worktree for a different feature
  15. git worktree add ~/worktree-b -b feature-b
  16. # List all worktrees
  17. git worktree list
  18. # Remove a worktree when done
  19. git worktree remove ~/worktree-a
  20. ```
  21. Each worktree is a separate folder with its own branch checked out. They all share the same Git history and `.git` directory, but have independent working directories.
  22. ## The `--cwd` Flag
  23. The `-c, --cwd <path>` flag tells Cline to run in a specific directory without changing your current location:
  24. ```bash
  25. # Run Cline in a different directory
  26. cline --cwd ~/worktree-a -y "refactor the authentication code"
  27. # Short form
  28. cline -c ~/worktree-b -y "add unit tests"
  29. ```
  30. This is the key to worktree workflows—you can run multiple Cline instances in different worktrees simultaneously from a single terminal.
  31. ## Pattern 1: Parallel Task Execution
  32. Run different tasks in parallel across multiple worktrees. Each task works on a separate branch in complete isolation.
  33. ### Example: Parallel Feature Development
  34. ```bash
  35. # Terminal 1: Update docs in worktree-a
  36. cline --cwd ~/worktree-a -y "read the last 10 changes using git show and update our README with them" &
  37. # Terminal 2: TypeScript migration in worktree-b
  38. cline --cwd ~/worktree-b -y "update the index.js to use typescript" &
  39. # Terminal 3: Refactoring in worktree-c
  40. cline --cwd ~/worktree-c -y "refactor the cli/ folder to be more modular" &
  41. # Wait for all to complete
  42. wait
  43. ```
  44. The `&` runs each command in the background, allowing all three to execute simultaneously.
  45. ### When to Use Parallel Execution
  46. **Perfect for:**
  47. - Multiple independent features
  48. - Bulk refactoring across different modules
  49. - Running tests in one worktree while developing in another
  50. - Trying multiple approaches to the same problem
  51. **Not ideal for:**
  52. - Tasks that modify the same files (merge conflicts likely)
  53. - Tasks that depend on each other's results
  54. - When you need to monitor progress closely
  55. ## Pattern 2: Cross-Worktree Context Piping
  56. Pipe output from one worktree as input to another. Use when a task in one worktree needs context from attempts in another worktree.
  57. ### Example: Learning from Failures
  58. ```bash
  59. # Try approach A in worktree-a, capture only the failure summary
  60. cline --cwd ~/worktree-a -y \
  61. "edit the index.ts to be better and then npm run. if it fails, output ONLY the failure summary. nothing else but the failure summary" \
  62. | cline --cwd ~/worktree-b -y \
  63. "i've tried to edit the index.ts in a different worktree but it failed. use a different approach for this work tree"
  64. ```
  65. **How it works:**
  66. 1. First Cline instance runs in `worktree-a`, attempts a change, tests it
  67. 2. If it fails, outputs just the failure summary
  68. 3. That summary is piped to a second Cline instance in `worktree-b`
  69. 4. Second instance sees the failure and tries a different approach
  70. ### When to Use Context Piping
  71. **Perfect for:**
  72. - A/B testing different solutions
  73. - Learning from failed attempts
  74. - Iterative refinement (try → analyze → try differently)
  75. - Comparing outputs across approaches
  76. **Not ideal for:**
  77. - Simple tasks that don't need cross-context
  78. - When both worktrees would succeed independently
  79. - Real-time collaboration (use parallel execution instead)
  80. ## Combining with Other CLI Features
  81. ### Different Models Per Worktree
  82. Use `--config` to run different models in different worktrees:
  83. ```bash
  84. # Cheap model for simple docs update
  85. cline --cwd ~/worktree-docs --config ~/.cline-haiku -y \
  86. "update README with latest changes"
  87. # Expensive model for complex refactoring
  88. cline --cwd ~/worktree-refactor --config ~/.cline-opus --thinking -y \
  89. "refactor authentication system for better security"
  90. ```
  91. This optimizes costs while maintaining quality where it matters.
  92. ### Task Isolation
  93. Keep long-running worktree sessions isolated by running each task against a different worktree path:
  94. ```bash
  95. # Run tasks in dedicated worktrees
  96. cline --cwd ~/worktree-a -y "long-running task"
  97. cline --cwd ~/worktree-b -y "another task"
  98. ```
  99. Each worktree has its own Git branch and working directory, so task history and changes stay separated without needing instance management.
  100. ### With YOLO Mode
  101. The `-y` (YOLO) flag is essential for worktree workflows:
  102. ```bash
  103. # Without -y: Opens interactive chat (blocks other tasks)
  104. cline --cwd ~/worktree-a "refactor code"
  105. # With -y: Runs autonomously (doesn't block)
  106. cline --cwd ~/worktree-a -y "refactor code" &
  107. ```
  108. For parallel execution, always use `-y` to avoid blocking on user approval.
  109. ## Real-World Workflow Example
  110. Here's a complete workflow showing how these patterns work together:
  111. ```bash
  112. # Setup: Create three worktrees
  113. git worktree add ~/cline-worktrees/feature-auth -b feature/authentication
  114. git worktree add ~/cline-worktrees/feature-api -b feature/api-endpoints
  115. git worktree add ~/cline-worktrees/fix-tests -b fix/failing-tests
  116. # Pattern 1: Run parallel independent tasks
  117. cline -c ~/cline-worktrees/feature-auth -y --config ~/.cline-sonnet \
  118. "implement JWT authentication" &
  119. cline -c ~/cline-worktrees/feature-api -y --config ~/.cline-sonnet \
  120. "create REST API endpoints for user management" &
  121. cline -c ~/cline-worktrees/fix-tests -y --config ~/.cline-haiku \
  122. "fix all failing unit tests" &
  123. wait
  124. echo "All parallel tasks complete!"
  125. # Pattern 2: Use piping for iterative refinement
  126. cline -c ~/cline-worktrees/feature-auth -y \
  127. "test the authentication with curl. output only errors if any" \
  128. | cline -c ~/cline-worktrees/feature-auth -y \
  129. "fix the authentication issues described in the input"
  130. # Merge successful changes back
  131. cd ~/cline-worktrees/feature-auth
  132. git checkout main
  133. git merge feature/authentication
  134. # Cleanup
  135. git worktree remove ~/cline-worktrees/feature-auth
  136. ```
  137. ## Best Practices
  138. <AccordionGroup>
  139. <Accordion title="Worktree Organization">
  140. - **Use a dedicated folder**: Create `~/cline-worktrees/` for all worktrees
  141. - **Meaningful branch names**: Use `feature/`, `fix/`, `refactor/` prefixes
  142. - **Clean up regularly**: Remove worktrees after merging branches
  143. </Accordion>
  144. <Accordion title="Task Isolation">
  145. - **Independent features only**: Don't parallelize tasks that touch the same files
  146. - **Test in isolation**: Each worktree should have its own test run
  147. - **Separate configs**: Use `.worktreeinclude` to copy `node_modules` and build artifacts
  148. </Accordion>
  149. <Accordion title="Resource Management">
  150. - **Monitor disk space**: Each worktree is a full checkout
  151. - **Limit parallel tasks**: Running too many simultaneously can slow your system
  152. - **Use background jobs wisely**: Track with `jobs` command, kill with `kill %1`, etc.
  153. </Accordion>
  154. <Accordion title="Error Handling">
  155. - **Check exit codes**: Use `|| echo "Task failed"` to catch errors
  156. - **Log outputs**: Redirect to files for debugging: `> worktree-a.log 2>&1`
  157. - **Graceful cleanup**: Always remove worktrees after tasks complete
  158. </Accordion>
  159. </AccordionGroup>
  160. ## Troubleshooting
  161. <AccordionGroup>
  162. <Accordion title="&quot;Branch already checked out&quot; error">
  163. Git doesn't allow the same branch in multiple worktrees. Solutions:
  164. - Use different branch names for each worktree
  165. - Remove the existing worktree first: `git worktree remove <path>`
  166. </Accordion>
  167. <Accordion title="Tasks not running in parallel">
  168. Make sure you're using:
  169. - `&` at the end of each command to background it
  170. - `-y` flag so Cline doesn't wait for approval
  171. - Different worktrees (not the same path)
  172. </Accordion>
  173. <Accordion title="Pipe not working as expected">
  174. Verify:
  175. - First command outputs to stdout (not stderr)
  176. - Second command reads from stdin (use `--` separator if needed)
  177. - Both commands use correct `--cwd` paths
  178. </Accordion>
  179. <Accordion title="Changes not appearing in worktree">
  180. Check:
  181. - You're in the right worktree: `git worktree list`
  182. - Files aren't gitignored
  183. - You committed/staged changes if needed
  184. </Accordion>
  185. </AccordionGroup>
  186. ## Related Documentation
  187. <Columns cols={2}>
  188. <Card title="Worktrees Overview" icon="code-branch" href="/features/worktrees">
  189. Complete guide to Git worktrees, VS Code integration, and .worktreeinclude
  190. </Card>
  191. <Card title="Model Orchestration" icon="layer-group" href="/cline-cli/samples/model-orchestration">
  192. Use different models strategically with --config and --thinking flags
  193. </Card>
  194. <Card title="CLI Reference" icon="terminal" href="/cline-cli/cli-reference">
  195. Complete documentation for --cwd and all other CLI flags
  196. </Card>
  197. <Card title="Headless Mode" icon="robot" href="/cline-cli/three-core-flows">
  198. Run Cline autonomously in scripts, CI/CD pipelines, and automated workflows.
  199. </Card>
  200. </Columns>