Skip to content

Use Worktrees

A worktree is a second checkout of your repository in another folder, on its own branch. Paneflow can open a pane in one, so an agent works on a branch while your project checkout stays where it is. Commits, branches and remotes are shared with the repository, since a worktree is a plain Git worktree.

Worktrees need a Git repository. In a project outside Git, the branch row and the tab's Branch menu don't appear.

Opening a Pane on a New Branch

  1. Add a pane. The New pane palette opens with the current branch above the list of agents.
  2. Click the branch and pick New branch….
  3. Fill in the form, then pick what to open under Open with: a terminal, an agent, or one of the project's custom commands.
FieldDescription
Branch nameThe branch to create. Leave it empty to start on a commit without a branch.
fromThe branch or commit to start from. It defaults to the branch of the project checkout.
WorktreeOn, separate folder: Paneflow creates a worktree. Off, switch this checkout: it switches the project checkout.

With Worktree on, Paneflow runs git worktree add -b <branch> <from>, binds the tab to the new folder, and starts what you picked there. The tab takes the branch name, unless you renamed it.

Paneflow's New pane palette with the New branch form: a branch name field, a from select, the Worktree toggle, and the list of agents to open with.

A Branch That Already Exists

Pick the branch in the list under New branch…, or type its name in the form. Git checks out a branch in one place at a time, so Paneflow goes to where it already is:

  • If the branch is checked out somewhere, in the project checkout or in any worktree, the pane opens there. Paneflow creates nothing.
  • Otherwise Paneflow creates a worktree for it.

from is ignored, since the branch already has its history.

Starting Without a Branch

Leave Branch name empty and Paneflow creates a worktree detached at the from commit. Its folder is named after the base and the commit, such as main-1a2b3c4.

When the work deserves a branch, right-click the tab and pick Create branch here…. Paneflow runs git switch -c <name> in that worktree, so uncommitted changes stay where they are. A name that already exists is refused.

Switching the Project Checkout Instead

Turn Worktree off and Paneflow switches the project checkout itself: git switch -c <branch> <from> for a new branch, git switch <branch> for an existing one, and git switch --detach <from> for an empty name. The pane opens in the project folder.

Switching moves the files under every pane of that checkout. Paneflow refuses while an agent is working there: "An agent is working in this checkout; create a worktree instead". Git can still refuse a switch that would overwrite changes, and the palette shows its error.

The toggle remembers your last choice in worktrees.for_new_branches.

The Tab Menu

Right-click a tab in a Git project to open its Branch section:

ItemDescription
New branch…Splits the tab and opens the same form for the new pane.
A branch or worktreeMoves the tab to that branch, in its checkout or a new worktree. Listed when there is more than one.
Create branch here…Names the branch of a detached worktree. Shown only for a tab in one.
Remove worktreeRemoves the tab's worktree after a confirmation. Shown for a tab in a worktree.

A tab also follows its terminal: cd into a worktree of the repository and the tab binds to it, cd back to the project checkout and it unbinds.

Where Worktrees Live

Paneflow creates worktrees under ~/.paneflow/worktrees, with one folder per repository and one per branch:

~/.paneflow/worktrees/
  paneflow-05926cc4/
    feat-agents-browser/
    fix-login/

The repository folder is its name followed by a hash of its full path. The branch folder is the branch name with every character other than letters, digits, ., _ and - replaced by -, so feat/login becomes feat-login. When two branches end up with the same name, the second one gets a hash suffix.

Paneflow writes nothing in your checkout. It marks a worktree as its own with a paneflow-owner file in the worktree's Git metadata, under .git/worktrees/<name>/. A worktree without that file is never removed by Paneflow, including one you made with git worktree add.

To change the root, click Change… in Settings > Worktrees, or set worktrees.dir in paneflow.json. A leading ~ is your home directory, and a path that isn't absolute is ignored. The new root applies to worktrees created from then on. PANEFLOW_HOME moves the whole ~/.paneflow directory, worktrees included.

Paneflow's Settings > Worktrees page: the worktree root, the automatic removal toggle, the keep limit, the list of managed worktrees, and the snapshots section.

Copying Ignored Files

A new worktree has every tracked file, but none of the files Git ignores, such as .env. Paneflow copies the ones a worktree needs to run.

List them in a .worktreeinclude file at the repository root, one path per line, relative to the root:

# .worktreeinclude
.env
.env.local
config/secrets.json
  • A folder is copied with everything in it, with or without a trailing /.
  • # starts a comment. There are no globs and no ! patterns: each line is a path.
  • An absolute path or a path with .. is skipped.
  • A file that already exists in the worktree is never overwritten.

Without the file, Paneflow copies every file at the root whose name starts with .env, and AGENTS.override.md.

Removing Worktrees

Each worktree carries its own files, dependencies and build caches, so Paneflow removes the ones you no longer use. It removes a worktree:

  • When you close the workspace it belongs to.
  • When you have more than the keep limit, 15 by default. The oldest go first. Paneflow checks the limit when it creates a worktree, when you add a workspace, and when it refreshes Git status.

A worktree is never removed automatically while a tab, a workspace or a live terminal session uses it. Turn Remove old worktrees automatically off in Settings > Worktrees and Paneflow removes nothing on its own.

To remove one by hand, click Remove next to it in Settings > Worktrees, or pick Remove worktree in the tab menu. When a tab or a terminal uses it, Paneflow asks first, then closes those tabs and stops those sessions. A worktree open as its own workspace has to be closed first.

Removing a worktree removes the folder, never the branch. Your commits stay on it.

Snapshots

Before removing a worktree, by hand or automatically, Paneflow saves its uncommitted changes as a snapshot. A worktree without changes leaves none.

A snapshot holds the changes to tracked files and the new files Git doesn't ignore. Ignored files, such as .env, aren't saved, and neither are editor buffers you haven't saved. If Paneflow can't take the snapshot, it keeps the worktree and says so.

Snapshots are Git commits stored under refs/paneflow/snapshots/, named after the branch and the time, such as refs/paneflow/snapshots/feat-login-1764460800. They never touch your branches and are never pushed. List them with Git:

git for-each-ref refs/paneflow/snapshots/

Settings > Worktrees lists the snapshots of the repositories you have open:

  • Restore recreates the worktree, puts the changes back as unstaged changes, copies the .worktreeinclude files again, and deletes the snapshot. It reuses the original folder when it's free, and the original branch when it isn't checked out elsewhere and hasn't moved. Otherwise the worktree comes back detached.
  • Delete asks for a second click, Confirm delete, then drops the snapshot for good.

Paneflow keeps snapshots until you restore or delete them.

From a Script

paneflow up, flows and the JSON-RPC API open panes in worktrees too. In a paneflow.workspace.toml, a pane takes these keys:

[[panes]]
command = "claude"
cwd = "."
worktree = "feat/login"
from = "main"
KeyDescription
worktreeThe branch. Requires cwd inside a Git repository.
fromThe base for a new branch. Ignored when the branch exists.
copy_envCopies the .worktreeinclude files. true by default.
setupA command to run in a new worktree before the pane starts.
setup_timeout_secsHow long setup may run. 300 by default.
worktree_teardown"auto" lets Paneflow remove the worktree, "keep" never does. "auto" by default.

In workspace.up and surface.split, the same settings go in a worktree object: branch, from, copy_env, setup, setup_timeout_secs and teardown.

Unlike the palette, a script reuses only a worktree Paneflow created. A branch checked out in the project checkout, or in a worktree you made, is refused:

pane 0: branch 'feat/login' already checked out at /home/you/code/app

Run paneflow up --dry-run to see, for each pane, whether Paneflow would create or reuse a worktree. When one pane fails, the worktrees already created for the others are removed. A setup that fails or times out still starts the pane, with a notice.

See Scripting for the rest of the workspace file.

What a Worktree Doesn't Isolate

A worktree separates files and branches, nothing else. Ports, databases, network access and your user's permissions are shared, so give each agent its own dev-server port. Files copied by .worktreeinclude can hold secrets. A worktree is not a sandbox.

The Changes tab shows a checkout's uncommitted changes against its HEAD. It doesn't compare a branch with its base. For that, run git diff main...feat/login.

Settings

KeyDefaultDescription
worktrees.dir~/.paneflow/worktreesWhere new worktrees are created.
worktrees.auto_removetrueRemoves worktrees on workspace close and past the keep limit.
worktrees.keep_limit15How many worktrees to keep, from 0 to 200.
worktrees.for_new_branchestrueWhether the Worktree toggle starts on.

Settings > Worktrees edits the same keys. The keep limit shows only while automatic removal is on.

See Also