Work with agents in worktrees
Choose where an agent changes files before you start a coding task. Use your current folder to work with existing edits, or a separate Git worktree to keep an independent task's changes apart. This article helps you choose isolation, prepare the worktree, bring reviewed changes back into your project, and clean up worktrees you no longer need.
Separate conversations don't automatically mean separate files. Chats in the same folder or worktree can change the same files, even when they have different conversation histories.
Choose folder or worktree isolation
The available choices depend on the session's harness and execution environment.
| Aspect | Current folder | New worktree |
|---|---|---|
| Best for | Working with existing files and unfinished edits. | Independent tasks or alternative implementations. |
| Where edits go | Directly into the selected folder. | Into a separate working directory and branch. |
| Starting point | The folder's current files, including uncommitted changes. | Committed files from the session's base branch. |
| Uncommitted and untracked files | Available in the folder. | Not copied from the original folder. |
| Integration | Edits are already in the selected folder. | Review and integrate the worktree branch afterward. |
For parallel implementation tasks, use a separate worktree for each task. For related conversations that should share edits, use the same folder or worktree. Forking a chat doesn't by itself create another worktree.
A worktree separates working files, not the agent's access to your machine or external services. Choose from the permission levels your harness offers, and use agent sandboxing for operating system-level restrictions.
Prepare a worktree
-
Use a Git repository with at least one commit and a harness that supports worktree isolation. Dev Container sessions work directly in the container workspace and don't offer New Worktree.
-
Commit the code the task needs. A new worktree doesn't include uncommitted or untracked files from the original folder.
-
Choose the session's base branch before starting work. Don't assume it is the branch currently checked out in your editor. For tasks you want to compare or integrate together, start each from the same known-good commit.
-
Prepare dependencies and local configuration in the new worktree before running the project.
Git-ignored files, such as installed dependencies and local configuration, are absent from new worktrees by default. Run your project's setup or configure which ignored files to copy. Only include configuration that is safe for the agent to access.
For large ignored folders, symlinking instead of copying is Experimental. Changes through a symlink affect the original folder, so don't use it for files that must stay isolated.
Worktrees can still share ports, databases, and other external resources. Use separate test resources or run conflicting checks one at a time.
Start a worktree session
Choose the interface that fits how you want to work:
| Interface | Workflow |
|---|---|
| Agents window | Choose New Worktree and a base branch in the session input. Coordinate sessions across projects or view several side by side. |
| Chat view Editor worktree support is experimental and might change or be removed. | In VS Code Insiders, enable editor worktree support for a single local Git folder. Choose New Worktree before the first prompt. The editor's folder location follows the chat you open. |
Each interface has its own controls and defaults. Follow the linked setup instructions, and confirm the session's working directory and branch before editing, running commands, or committing.
Review and integrate changes
Switching chats or opening another worktree doesn't merge changes into your original folder. Review each task's complete changes, including untracked files, and run the relevant checks in its worktree. Follow Review AI-generated code edits for the controls available in each interface.
Commit the changes you want to keep, then merge the worktree branch into your intended destination branch or use your team's pull request workflow. The Agents window also provides integration actions for supported sessions.
After combining changes, rerun the relevant checks on the destination branch. Keep the sessions and worktrees available until you have preserved the changes and validated the combined result.
Clean up session worktrees
Reclaim disk space from inactive or leftover agent-managed worktrees while retaining their branches. In VS Code Insiders, the same cleanup editor is available in the regular editor window and the Agents window, including for supported connected remote hosts.
Cleanup marks listed sessions done, commits nonignored changes, including untracked files, to their branches, and schedules the worktree folders for removal. Branches and session history are retained, so restoring a session re-creates its worktree. Worktrees used by other sessions or chats are protected.
Ignored files in removed worktrees are deleted. Copy out any local configuration or other ignored files you need before confirming cleanup.
-
Run Chat: Open Worktree Cleanup from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) in either window.
-
Choose an inactivity filter and, if needed, a workspace.
-
Review the selected worktrees, including their hosts, branches, paths, and estimated sizes. Clear any selected worktrees you want to keep. Cleanup can include sessions outside the editor's current workspace.
-
Save open file edits in the selected worktrees, then select the cleanup button.
-
Review the confirmation, including any remote hosts, then select Clean Up.
Cleanup also includes eligible leftovers from done sessions and agent-managed worktrees without a listed session. The latter retain their branches but have no conversation to restore. For worktrees you manage yourself, see Remove a worktree.
Clean up remote worktrees and handle failures
Remote cleanup requires a connected host that supports it and removes files on that host, not your client machine. Check the host as well as the path, especially when hosts use identical folder paths.
If the host is unavailable or unsupported, connect or update it, then refresh. If the connection drops during cleanup, reconnect and refresh to verify the result.
For failures, follow the worktree's status and error: a retry might already be scheduled, or you can select an eligible worktree to retry. Resolve any use by another session or chat first. Unknown size estimates and pending or failed cleanup don't confirm removal.
Configure cleanup suggestions
In Automatic Cleanup, use chat.agentSessions.sessionStorageCleanupSuggestion.enabled (Experimental) to control suggestions in the Agents window. Suggestions aren't shown in the regular editor window and don't monitor remote free disk space. See the AI settings reference for defaults and thresholds.
You can also configure automatic completion and deletion retention. These aren't required for manual cleanup.