# Zed Worktrees: Create and Switch Git Worktrees in Zed

_Zed manages Git worktrees from a worktree picker in the title bar. Open it with the `git: worktree` action to create a linked worktree, switch to it, or delete one you no longer have open._

## The Fix

Open the worktree picker. It sits in the title bar next to the project picker, or you can run `git: worktree` from the command palette. The default keybinding is `cmd-ctrl-w` on macOS and `alt-ctrl-shift-w` on Linux and Windows.

From the picker you can:

- **Create** a linked worktree from the current branch or the default branch. Type a name or let Zed pick one.
- **Switch** the current workspace to an existing worktree, or open it in a new window. Search by worktree name or branch name.
- **Delete** a linked worktree that is not open in the project.

New worktrees start in detached `HEAD`. Use the branch picker next to the worktree picker to create a branch or check out an existing one before you commit. Zed does this so the same branch is never checked out in two worktrees. If you pick a branch that another worktree holds, the worktree stays detached.

By default Zed creates worktrees under `../worktrees`, relative to the repository's working directory. The `git.worktree_directory` setting changes that location.

## Copy .env Into New Worktrees

A new worktree has none of the files your `.gitignore` excludes. Add a task with the `create_worktree` hook to `.zed/tasks.json`. Zed runs it after it creates a linked worktree:

```json
[
  {
    "label": "copy .env into new worktree",
    "command": "cp",
    "args": ["$ZED_MAIN_GIT_WORKTREE/.env", "$ZED_WORKTREE_ROOT/.env"],
    "hooks": ["create_worktree"],
    "reveal": "no_focus",
    "hide": "on_success"
  }
]
```

`ZED_WORKTREE_ROOT` is the new worktree. `ZED_MAIN_GIT_WORKTREE` is the original repository's working directory.

## Agents in Zed Worktrees

When two agent threads might edit the same files, start one in a new worktree from the picker. Each thread then has its own checkout. Review the diff when the agent finishes and merge through your usual Git workflow. [Run parallel agents in OpenCode and Zed](/learn/how-to/run-parallel-agents-in-opencode-and-zed) walks through the Threads Sidebar.

Moving a thread to Thread History saves the worktree's Git state and removes the worktree from disk, unless another active thread uses it. Restoring the thread brings the worktree back.

## How Treq Handles It

Treq creates its own [workspaces](/docs/concepts/workspaces) under `.treq/workspaces/` instead of Git worktrees, one per agent or task. Each workspace is an ordinary directory, so you can open it in Zed like any other folder.

## Sources

- [Zed Git docs: worktrees](https://zed.dev/docs/git)
- [Zed parallel agents: worktree isolation](https://zed.dev/docs/ai/parallel-agents)
- [Zed tasks: hooks](https://zed.dev/docs/tasks)

## Related

- [Git worktrees in VS Code](./worktrees-in-vscode.md)
- [Codex worktrees](./codex-worktrees.md)
- [What are Git Worktrees?](/learn/concepts/git/git-worktrees)
