NEWCloud agents are live.See how

Tooling

Worktree

A second working copy of a git repository checked out to its own branch in its own directory, sharing one .git history. Worktrees are what let several agents edit the same project at once without stepping on each other's files or fighting over the checked-out branch.

At a glance

Command
git worktree add
Shares
History, objects, stash, hooks
Separate
Directory, index, HEAD, branch
Rule
One branch per worktree at a time
Cleanup
git worktree remove or prune

How it works

A normal clone has one working directory attached to one .git directory. git worktree add creates another directory linked to the same repository, with its own checked-out branch, index, and HEAD. Commits made in any worktree land in the shared object store immediately, so there is nothing to push or pull between them.

By default git refuses to check out a branch that is already checked out in another worktree. That rule is what makes worktrees safe for parallel work: two sessions cannot commit to the same branch from different directories by accident.

Why it matters

Two agents in one directory overwrite each other's edits, trip over each other's half-finished changes when running tests, and fight over which branch is checked out. Separate clones avoid that but duplicate the history and drift apart. Worktrees give each agent an isolated directory and branch at the cost of only the checked-out files.

Some state is still shared because it lives in .git, notably the stash stack, hooks, and config. Running git stash in one worktree pushes onto the stack every other worktree sees.

Each worktree also needs its own setup. Untracked files are not copied, so a fresh worktree has no .env, no installed dependencies, and no build output. A short setup script that copies the env file and installs packages saves every agent from rediscovering this. If several worktrees run dev servers at once, give each its own port.

Worktree vs. branch vs. clone

A branch is a pointer to a commit; it gives you no second place to edit files. A clone is a whole independent repository. A worktree sits between them: a separate checkout that shares one history, which is usually the right unit for running several agents on one repo.

Example

Create, list, and remove a worktree
git worktree add ../myapp-fix-login -b fix/login
cp .env ../myapp-fix-login/
git worktree list
git worktree remove ../myapp-fix-login

Common mistakes

  1. Expecting untracked files like .env and node_modules to appear in a new worktree, then debugging a test failure that is only missing setup.
  2. Using git stash across worktrees, where a later pop elsewhere can apply another branch's changes.
  3. Deleting worktree directories by hand instead of git worktree remove, which leaves stale records until git worktree prune.

In Fredrin

Every Fredrin ticket runs on its own branch in its own git worktree, a real checkout you can cd into, so several agent sessions can work on one project at once.

Branches and worktrees in Fredrin

Frequently asked questions

Can two worktrees have the same branch checked out?

Not by default. Git refuses to add a worktree for a branch already checked out elsewhere unless you pass --force, which defeats the purpose.

Do worktrees take up much disk space?

Only the checked-out files and some metadata, since history is shared. Dependency folders like node_modules are duplicated unless your package manager links them.

How do I delete a worktree?

Run git worktree remove <path>. If the directory is already gone, git worktree prune cleans up the stale record.

Sources

Know the words. Run the work.