worktrees.mdx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ---
  2. title: "Worktrees"
  3. sidebarTitle: "Worktrees"
  4. ---
  5. Worktrees let you work on multiple branches simultaneously, each in its own folder. This enables Cline to work on tasks in parallel across separate VS Code windows, or lets Cline work independently while you continue coding in your main workspace.
  6. ## What Are Git Worktrees?
  7. A Git worktree is a linked copy of your repository in a separate folder, checked out to a specific branch. All worktrees share the same Git history and `.git` directory, but each has its own working directory with different code checked out.
  8. Key concepts:
  9. - **Main worktree**: Your original repository folder where the `.git` directory lives
  10. - **Linked worktrees**: Additional folders you create, each checked out to a different branch
  11. - **Shared history**: All worktrees share commits, branches, and Git configuration
  12. <Tip>
  13. Unlike regular branch switching, worktrees let you have multiple branches checked out at the same time in different folders. This means you can have VS Code windows open for different features simultaneously.
  14. </Tip>
  15. ## Why Use Worktrees with Cline?
  16. Worktrees solve a common problem: **Cline takes over your VS Code window while working on a task**. With worktrees, you can:
  17. 1. **Run Cline in parallel** - Have Cline work on multiple tasks simultaneously, each in its own worktree and VS Code window
  18. 2. **Keep working while Cline works** - Let Cline handle a task in a separate worktree while you continue coding in your main workspace
  19. 3. **Isolate experimental changes** - Test risky changes in a worktree without affecting your main branch
  20. 4. **Quick context switching** - Jump between features without stashing or committing incomplete work
  21. ## Getting Started
  22. ### Quick Launch (Recommended)
  23. The fastest way to start using worktrees is the **New Worktree Window** button on Cline's home screen:
  24. 1. Click **New Worktree Window** on the home screen
  25. 2. Enter a branch name and folder path (defaults are auto-filled)
  26. 3. Click **Create & Open**
  27. A new VS Code window opens with your worktree, and Cline automatically opens ready to work.
  28. <Tip>
  29. The home screen also shows your current branch and worktree path. Click it to open the full Worktrees view.
  30. </Tip>
  31. ### Full Worktrees View
  32. For more control, open the full Worktrees view by clicking the **Worktrees** button in the Cline sidebar header, or by clicking your current branch info on the home screen:
  33. <Steps>
  34. <Step title="Create a New Worktree">
  35. Click **New Worktree** at the bottom of the view. Enter a branch name and path (defaults are auto-filled).
  36. </Step>
  37. <Step title="Open in New Window">
  38. Once created, click the **Open in new window** button to open the worktree in a separate VS Code window. Cline will automatically open in the new window.
  39. </Step>
  40. </Steps>
  41. ## Typical Workflow
  42. Here's how a typical worktree session looks:
  43. <Steps>
  44. <Step title="Create a new worktree">
  45. Click **New Worktree Window** on the home screen or use the Worktrees view. A new VS Code window opens with Cline ready to go.
  46. </Step>
  47. <Step title="Do your work">
  48. Work on your feature or let Cline handle a task. Make commits as you go.
  49. </Step>
  50. <Step title="Close the worktree window">
  51. When you're done, close the worktree's VS Code window.
  52. </Step>
  53. <Step title="Merge from your primary worktree">
  54. Back in your main VS Code window, open the Worktrees view and click the **merge button** on the worktree you just worked in. This merges the branch and optionally deletes the worktree.
  55. </Step>
  56. </Steps>
  57. ## Managing Worktrees
  58. ### Viewing Worktrees
  59. The Worktrees view shows all worktrees for your repository:
  60. - **Current**: The worktree you're currently in (highlighted)
  61. - **Main**: The primary worktree where your `.git` directory lives (cannot be deleted)
  62. - **Locked**: Worktrees that are locked to prevent accidental deletion
  63. ### Opening Worktrees
  64. Each worktree has two open options:
  65. - **Open in current window**: Replace your current workspace with the worktree
  66. - **Open in new window**: Open the worktree in a separate VS Code window (recommended for parallel Cline sessions)
  67. Either way, Cline automatically opens in the new workspace, ready to start a task.
  68. ### Deleting Worktrees
  69. Click the trash icon on any linked worktree to delete it. A confirmation dialog will show you exactly what will be deleted:
  70. - The branch itself
  71. - All project files in the worktree folder
  72. <Warning>
  73. Deleting a worktree permanently removes the branch and all files in that folder. Make sure any important changes are committed and pushed first.
  74. </Warning>
  75. <Note>
  76. You cannot delete the main worktree. It's the primary repository where your `.git` directory lives.
  77. </Note>
  78. ### Merging Worktrees
  79. When you're done working in a worktree and ready to merge your changes back to the main branch:
  80. 1. Click the **merge icon** (git merge symbol) on any linked worktree
  81. 2. Review the merge details in the confirmation modal
  82. 3. Choose whether to delete the worktree after merging
  83. 4. Click **Merge**
  84. #### Handling Merge Conflicts
  85. If your branch has conflicts with the main branch, Cline will detect them and show you the conflicting files. You have two options:
  86. 1. **Ask Cline to Resolve & Merge** - Creates a new Cline task with a prompt asking Cline to resolve the conflicts, complete the merge, and clean up the worktree
  87. 2. **Resolve Manually** - Close the modal and resolve conflicts yourself using your preferred Git tools
  88. <Tip>
  89. The "Ask Cline to Resolve" option is particularly useful for complex conflicts. Cline will analyze the conflicting files and attempt to merge them intelligently based on the intent of both branches.
  90. </Tip>
  91. ## .worktreeinclude: Automatic File Copying
  92. When you create a new worktree, it starts with a fresh checkout—no `node_modules`, no build artifacts, no IDE settings. This means you'd normally need to run `npm install` or similar setup commands.
  93. The `.worktreeinclude` file solves this by automatically copying specified files to new worktrees.
  94. ### How It Works
  95. 1. Create a `.worktreeinclude` file in your repository root
  96. 2. Add glob patterns for files you want copied (using `.gitignore` syntax)
  97. 3. When Cline creates a new worktree, files matching **both** `.worktreeinclude` **and** `.gitignore` are copied automatically
  98. <Note>
  99. Only files that are both matched by `.worktreeinclude` AND listed in `.gitignore` are copied. This prevents accidentally duplicating tracked files.
  100. </Note>
  101. ### Example `.worktreeinclude`
  102. ```gitignore
  103. # Copy node_modules to avoid npm install
  104. node_modules/
  105. # Copy IDE settings
  106. .vscode/
  107. # Copy build cache
  108. .next/
  109. dist/
  110. # Copy environment files (if gitignored)
  111. .env.local
  112. ```
  113. ### Creating a `.worktreeinclude` File
  114. The Worktrees view will show a tip if you don't have a `.worktreeinclude` file. If you have a `.gitignore`, you can click **Create from .gitignore** to create one pre-filled with your gitignore contents. Then edit it to keep only the patterns you want copied.
  115. <Tip>
  116. For most JavaScript/TypeScript projects, just including `node_modules/` in your `.worktreeinclude` saves significant setup time for each new worktree.
  117. </Tip>
  118. ### Pro Tip: Symlink to .gitignore
  119. Since `.gitignore` usually contains most of the files you'd want copied to new worktrees (dependencies, environment files, build caches, etc.), you can create a symlink so they stay in sync automatically:
  120. ```bash
  121. # In your repository root
  122. ln -s .gitignore .worktreeinclude
  123. ```
  124. Now whenever you update your `.gitignore`, your `.worktreeinclude` will have the same patterns. This is especially useful for projects where gitignored files are exactly what you want copied—no need to maintain two separate files.
  125. <Note>
  126. If you need different patterns than your `.gitignore`, create a regular `.worktreeinclude` file instead of a symlink.
  127. </Note>
  128. ## Best Practices
  129. <AccordionGroup>
  130. <Accordion title="For Parallel Cline Sessions">
  131. 1. **Create purpose-specific worktrees** - Name branches clearly (e.g., `cline/refactor-auth`, `cline/add-tests`)
  132. 2. **Open in new windows** - Always use "Open in new window" for true parallelism
  133. 3. **Use .worktreeinclude** - Set up automatic file copying to reduce setup time
  134. </Accordion>
  135. <Accordion title="For Solo Development">
  136. 1. **Keep your main branch clean** - Use worktrees for experimental or risky changes
  137. 2. **Quick feature switches** - Instead of stashing, create a worktree for interruptions
  138. 3. **Review in isolation** - Create worktrees to review PRs without disrupting your work
  139. </Accordion>
  140. <Accordion title="Worktree Hygiene">
  141. 1. **Delete unused worktrees** - Remove worktrees when their branches are merged
  142. 2. **Use meaningful names** - Branch names should indicate the worktree's purpose
  143. 3. **Check for stale worktrees** - Periodically review and clean up old worktrees
  144. </Accordion>
  145. </AccordionGroup>
  146. ## Limitations
  147. Worktrees are not available in certain workspace configurations:
  148. - **Multi-root workspaces**: If you have multiple folders open in VS Code, worktrees are disabled. Open a single repository folder instead.
  149. - **Subfolder of a repository**: If you've opened a subfolder within a Git repository (not the root), worktrees are disabled. Open the repository root folder instead.
  150. The Worktrees view will display a message explaining the limitation if either of these applies to your workspace.
  151. ## Using Worktrees with Cline CLI
  152. Cline CLI's `--cwd` flag unlocks powerful command-line worktree workflows:
  153. - **Parallel execution**: Run multiple Cline instances simultaneously in different worktrees
  154. - **Context piping**: Pipe output from one worktree as input to another for iterative refinement
  155. - **Combined with other features**: Use with `--config` for different models per worktree, or `--thinking` for deep analysis
  156. Example:
  157. ```bash
  158. # Run parallel tasks in different worktrees
  159. cline --cwd ~/worktree-a -y "refactor authentication" &
  160. cline --cwd ~/worktree-b -y "add unit tests" &
  161. wait
  162. ```
  163. For complete CLI worktree patterns and examples, see [Worktree Workflows](/cline-cli/samples/worktree-workflows).
  164. ## Troubleshooting
  165. <AccordionGroup>
  166. <Accordion title="Branch already exists error">
  167. Git doesn't allow the same branch to be checked out in multiple worktrees. Either:
  168. - Use a different branch name
  169. - Delete the existing worktree using that branch
  170. </Accordion>
  171. <Accordion title="Worktree folder already exists">
  172. The path you specified already contains files. Choose a different path or delete the existing folder first.
  173. </Accordion>
  174. <Accordion title="Can't delete worktree">
  175. If a worktree is locked, you'll need to unlock it first using `git worktree unlock <path>` in the terminal. If the worktree has uncommitted changes, you may need to use force delete.
  176. </Accordion>
  177. <Accordion title=".worktreeinclude files not copying">
  178. Make sure the files you want copied are:
  179. 1. Listed in your `.worktreeinclude` file
  180. 2. Also listed in your `.gitignore` (only gitignored files are copied)
  181. 3. Actually exist in your current worktree
  182. </Accordion>
  183. </AccordionGroup>
  184. ## Technical Details
  185. <AccordionGroup>
  186. <Accordion title="How Worktrees Work Internally">
  187. - Worktrees are a native Git feature (`git worktree` command)
  188. - All worktrees share the same `.git` directory and object database
  189. - Each worktree has its own index, working directory, and HEAD
  190. - Worktree list is stored in `.git/worktrees/`
  191. </Accordion>
  192. <Accordion title="Storage Considerations">
  193. - Each worktree contains a full checkout of the repository
  194. - `.worktreeinclude` can significantly increase worktree size (e.g., copying `node_modules`)
  195. - Consider your disk space when creating many worktrees
  196. </Accordion>
  197. <Accordion title="Relationship with Checkpoints">
  198. Worktrees are separate from Cline's [checkpoint system](/core-workflows/checkpoints). Each worktree has its own checkpoint history. Checkpoints track changes within a single worktree, while worktrees let you work across multiple branches simultaneously.
  199. </Accordion>
  200. </AccordionGroup>
  201. Worktrees unlock true parallel development with Cline. Create a worktree, open it in a new window, and let Cline work independently while you continue coding!