# Worktrees

> Give zelk a Git worktree of its own, so its work stays off your checkout until you want it.

A Git worktree is a second checkout of the same repository, on its own
branch. Working in one, zelk can change files and run builds while your
own checkout stays as it is, and you can run several sessions side by side.

## Start in a worktree

```sh
zelk -worktree fix-42                         # the terminal UI, in .zelk/worktrees/fix-42
zelk exec -mode accept-edits -worktree fix-42 "fix #42"
zelk -worktree=                               # zelk picks a name
```

`-worktree NAME` works in `.zelk/worktrees/NAME` on the branch
`worktree-NAME`, creating both from the current `HEAD` if they don't exist
yet, and using them as they are if they do. Give it before the task.

## Review a pull request

Give `-worktree` a pull or merge request instead of a name, and zelk
fetches that change from `origin` into a worktree of its own:

```sh
zelk -worktree '#1234'                                          # into pr-1234
zelk -worktree https://github.com/OWNER/REPO/pull/1234
```

Quote `#1234`: the shell reads an unquoted `#` as a comment.

## Bring ignored files along

A new worktree has only what Git tracks. To copy in ignored files a build
needs, such as `.env`, list them in `.worktreeinclude` at the repository
root, one pattern per line as in `.gitignore`:

```text title=".worktreeinclude"
.env
config/local.yaml
```

Only files that are both ignored and listed are copied.

## Continue a worktree's session

```sh
zelk -worktree fix-42 -c                 # the terminal UI: that worktree's last session
zelk -worktree fix-42 -r                 # pick among its sessions
zelk exec -worktree fix-42 resume --last "go on"
```

A session started anywhere inside a worktree shares the main checkout's
memory, and its skills and agents unless the worktree has its own.

## Leaving

**`zelk exec`** removes a worktree only when the agent leaves it with
`ExitWorktree` and asks to remove it, once the session's new place is
recorded. Otherwise it keeps the worktree and its branch. When it exits, after
an error or an interrupt too, it prints to standard error the command that
takes you back to the main checkout and continues that session:

```text
zelk exec: return with: cd '/home/you/repo' && zelk exec '-mode' 'accept-edits' -state '/home/you/.local/state/zelk' -worktree 'fix-42' resume --last
```

Run it with the same environment, adding a task to continue.

**The terminal UI** cleans up when you quit normally. A clean worktree
whose name zelk generated is removed, with its branch if zelk created it,
unless you launched zelk directly in it; then it is only offered for
removal, like any other.
Any other worktree gets a cleanup sheet describing what removing it would
delete, with **Keep worktree** selected: Enter or Esc keeps it, and
**Remove worktree** discards that work. zelk refuses to remove a worktree
another process holds, or one it can't account for, and always keeps a
branch it can't show it made. After an error or a signal, the worktree is
kept.

Removing a worktree you kept is up to you: `git worktree remove
.zelk/worktrees/fix-42`, then `git branch -D worktree-fix-42` once you no
longer need the branch.

## On Windows

Worktrees work on Windows with Git for Windows installed: zelk runs its
`git.exe`, as for every other git inspection it makes. A worktree is a
directory under `.zelk\worktrees`, so its name must be one Windows can
hold. zelk refuses `con`, `prn`, `aux`, `nul`, `com1` to `com9` and `lpt1`
to `lpt9` (with or without an extension, in any case), a trailing `.`, and
the characters Windows reserves. It also refuses a name that differs from
an existing worktree's only in case, since NTFS would put both in one
directory: `-worktree Feature` after `-worktree feature` says so instead of
reopening the first.

A worktree's holder is recognised by its process and the boot it started in.
If zelk can't tell whether a holder is still running, because Windows won't
let it look at that process, the worktree counts as held, never as free,
and zelk doesn't reclaim or remove it.

## Where to go deeper

- [`-worktree` in the CLI reference](/reference/cli/)
- [Sessions, rewind and checkpoints](/cli/sessions/)
