Skip to content

Git worktrees

Use a git worktree when more than one person or agent session is working in this repo at once. Each linked worktree has its own checked-out files, branch, HEAD, and index, while the repository objects and local branch refs remain shared. That isolates uncommitted edits and staging, but deleting or moving a shared branch ref still affects every checkout.

Worktrees live outside the checkout, in one .capell-wt root shared by the App, Core and companion-package repositories. Set CAPELL_WORKTREE_ROOT to that root (the App’s ./capell worktree reads the same variable); otherwise use ../.capell-wt. When the App and Core checkouts sit in different parent directories, the fallback produces a second root whose orphaned worktrees nobody cleans up, so set the variable. Never create one inside the repository — an in-tree worktree is deleted by git clean -fdx run in the primary checkout, and every tree-wide scan then walks two copies of the source.

Terminal window
git worktree add "${CAPELL_WORKTREE_ROOT:-../.capell-wt}/core-my-feature" -b feature/my-feature

First: are you running PHP on the host, or in Docker?

Section titled “First: are you running PHP on the host, or in Docker?”

This decides how you provision vendor/, and getting it wrong costs you a session.

Earlier versions of scripts/init-worktree.sh built vendor/ from host-absolute symlinks into the primary checkout. The container bind-mounts the worktree at /home/capell/current and does not mount the primary checkout, so every symlinked package dangled inside it, and PHP reported a fatal on the first require of a dangling path (require(/Users/.../vendor/symfony/deprecation-contracts/function.php): Failed to open stream) before a single test ran. The script no longer creates symlinks, so that layout cannot occur from a fresh run. scripts/check-vendor-integrity.php refuses a leftover one.

For tooling run through ./capell, the normal route is still a real install in the worktree:

Terminal window
./capell up
./capell composer install

./capell derives a per-worktree Compose project from the directory name, so this stack is isolated from the primary checkout’s. The install is a real, self-contained vendor/ that resolves inside the container, and with a warm Composer cache it is not slow — a measured cold-worktree run on 2026-08-19 took 41 seconds, plus a few seconds for ./capell up.

Remember to ./capell down when the worktree is idle. Parallel worktree stacks compound.

Host-only. See Docker worktrees first if you run tooling through ./capell.

A fresh worktree has no vendor/, and a full composer install costs several minutes and about 1.5 GB. Run this instead:

Terminal window
bash scripts/init-worktree.sh --host-only

It clones the primary’s vendor/ into the worktree (copy-on-write on APFS, so it takes seconds and almost no extra disk), verifies itself, and refuses to leave a broken vendor/ behind. Pass --force to rebuild an existing one. --host-only is accepted for older callers and has no effect.

This is the part that matters, because the failure is silent.

Composer’s generated autoloader computes $baseDir from __DIR__ inside vendor/composer/, and PHP resolves symlinks when evaluating __DIR__. If vendor/ — or just vendor/composer/ — is a symlink into the primary checkout, $baseDir becomes the primary checkout. Every Capell\* class then loads from the primary tree. Your worktree’s edits are invisible, and the suite passes while exercising completely different code. On a shared checkout it is worse: you are testing another session’s uncommitted work.

scripts/init-worktree.sh avoids this by giving the worktree a real, owned vendor/:

PathTreatmentWhy
vendor/composer/, vendor/autoload.phpreal copy$baseDir must resolve to this worktree
vendor/bin/real copybinary proxies must point at this worktree’s autoloader
vendor/<vendor>/<package>/real copycopy-on-write clone, so no package is a link into another checkout

The provisioner regenerates the copied autoloader from the worktree’s manifest, including development source mappings. Autoload regeneration disables Composer scripts and plugins and writes only the owned loader.

Because every package is a real tree inside the worktree, code that walks upward from inside vendor/ and Pest’s architecture presets (which exclude the checkout’s vendor/) behave as they do in a normal install. The copy is a snapshot of the primary’s vendor/ at provisioning time: when the primary’s dependencies change, re-run bash scripts/init-worktree.sh --force in each worktree rather than expecting it to follow.

scripts/check-vendor-integrity.php (run first by composer clear and by the Pest bootstrap) fails when vendor/ or any vendor/<vendor>/<package> resolves outside the checkout, so a legacy symlinked layout is refused instead of silently testing stale code.

Verify before trusting a test result:

Terminal window
php -r 'require "vendor/autoload.php"; echo (new ReflectionClass("Capell\Core\Enums\ImageSourceType"))->getFileName(), PHP_EOL;'

The path must be inside your worktree. If it points at the primary checkout, delete vendor/ and start again — every green test you have seen is meaningless.

The worktree’s vendor/ is its own copy, so Composer commands here cannot write through into the primary checkout. They will, however, diverge from the primary’s lock unless that is intended; manage dependencies in the primary, then re-run scripts/init-worktree.sh --force here.

scripts/init-worktree.sh gives the worktree its own node_modules/ as well as its own vendor/. It needs one: composer preflight runs prettier and eslint, so without node_modules/ the whole preflight exits before a single PHP stage runs, and the error names npm rather than the real cause.

Unlike vendor/, this is not shared. The script clones the directory copy-on-write, which on APFS shares the underlying blocks with the primary checkout until something writes — about a second, and almost no disk. On a filesystem without clone support it falls back to npm ci. An existing node_modules/ is left alone.

The warning above about dependency-mutating Composer commands does not apply to npm. Because node_modules/ is a real directory rather than a symlink, npm install and npm ci in a worktree cannot reach the primary checkout.

That isolation is the reason for the clone. npm ci deletes node_modules/ before it installs, so had the directory been symlinked, one npm ci in one worktree would have wiped the primary checkout’s copy out from under every other worktree and session using it.

In a Docker worktree, run them through the wrapper, which supplies the testing environment the package database guard requires:

Terminal window
./capell pest packages/frontend/tests/Feature/SomeTest.php --configuration=phpunit.xml --compact

./capell exec vendor/bin/pest ... skips that environment and fails with Refusing to run Capell package Pest tests against database [capell_4].

Before the first test or preflight in a fresh worktree, install its dependencies:

Terminal window
./capell up
./capell composer install

The wrapper checks for vendor/bin/pest before test and preflight commands and fails with this bootstrap instruction when it is absent. Never symlink vendor/ from another checkout: Composer’s autoloader must resolve Capell classes from the current worktree. Use bash scripts/init-worktree.sh --host-only only when all tooling will run on the host.

On the host it is identical to the primary checkout, but note that this repo’s tooling needs an explicit memory limit — PHP’s 128 MB default causes a fatal that looks like a broken setup:

Terminal window
composer test:unit

For a narrower run:

Terminal window
vendor/bin/pest --compact --configuration=phpunit.xml packages/core/tests/Unit

Do not pass -d memory_limit to Pest: phpunit.xml sets the limit through <ini>, which overrides the command-line flag in every process (see CI). PHPStan has no such file, so it still needs the flag:

Terminal window
php -d memory_limit=2G vendor/bin/phpstan analyse --no-progress packages/core/src

Each worktree has its own index. Path-scoped commits are still useful when more than one task is active in the same worktree:

  • Commit by pathspec — git commit -F - -- path/one path/two. Plain git add X && git commit commits the whole index, including unrelated files staged in that checkout.
  • For a new file, git add -N <path> first, or the pathspec commit errors.
  • Verify immediately with git show --stat: the file count must match your pathspec.
  • Verify the intended slice, inspect the staged paths and commit only that slice. Isolation protects you from other sessions; it does not replace verification.

Several sessions may share one checkout, and the working tree and index are then shared state. Another session’s git checkout, reset --hard or merge silently reverts uncommitted edits, so:

  • Run git branch --show-current immediately before every commit. The checkout can be switched to another feature branch mid-session; note which branch a commit landed on.
  • Verify each intended slice and inspect the index before committing it. Prefer an isolated worktree when another session is writing.
  • Install the snapshot hooks once from the primary checkout with python3 scripts/hooks/install-pre-commit.py. Linked worktrees share that hook; the installer refuses to change shared hooks from a linked worktree. The hook checks the active commit index in a private snapshot; pre-push checks each pushed range against its own target-commit snapshot. The debugging-method and do-not-commit guards run at both stages and in the PHP Quality workflow over the changed range. Neither hook stashes the original checkout. Existing legacy hooks are copied and run in that snapshot; review any legacy hook first because arbitrary code can still write absolute paths outside it. If a formatter changes the snapshot, the commit stops: coordinate with other writers before formatting, then review the result and re-stage.
  • The commit snapshot refuses merge commits, unmerged or sparse indexes, staged symlinks, submodules, clean/smudge filters such as LFS, checkout encoding or ident substitutions, unspecified text line endings and local info/attributes; it also requires the corresponding host dependencies for PHP or frontend checks. The installed Composer and npm graphs must agree with the snapshot manifests and lock files. Dependency trees are copied and checked for concurrent changes; their temporary disk demand depends on the installed graph. Shared dependency symlinks and path dependencies that cannot be represented inside the snapshot are refused; provision a coherent, supported dependency graph before retrying. It does not bypass checks or change Git configuration. The known hook configuration selects the needed dependency trees by staged file type; an existing legacy hook or an unfamiliar hook configuration or local hook implementation conservatively checks both. When changing the hook set or its scripts, review and refresh the fingerprints in scripts/hooks/pre-commit.py to restore selective checks. Dependencies of the listed scripts need the same review.
  • Both snapshots refuse symlinks that escape the snapshot. The push snapshot also refuses submodules and the same unsupported checkout attributes. For origin branches, PHP analysis requires the pushed commit to be checked out and refuses uncommitted edits to changed PHP files.
  • Reinstall the snapshot entrypoints after running pre-commit install for either hook type: the framework’s entrypoint reintroduces its worktree stash. A missing hook in a newly cloned checkout is not protected until installation. Coordinate installation with every linked worktree: the shared launcher loads the entrypoint from the invoking checkout, so older branches without these scripts cannot commit or push until they include the hook tooling.
  • A commit that sweeps in another session’s staged files can record an older copy of their work. Stop and preserve the commit and current files; inspect the impact and obtain the required approval before any reset or revert.

Remove scratch worktrees when they have served their purpose, and delete a merged branch locally and on the remote in the same pass. Only remove what you created: a worktree whose HEAD matches no remote branch (git branch -r --contains <sha>) holds unpushed work, and uncommitted files count too.

Auditing branches in a squash-merge repository

Section titled “Auditing branches in a squash-merge repository”

Pull requests are squash-merged, so git branch --merged and git merge-base --is-ancestor report merged branches as unmerged: branch commits never become ancestors of main. Instead:

  1. No-op merge test: git merge-tree --write-tree origin/main <branch>. If the resulting tree equals git rev-parse origin/main^{tree}, the branch is fully merged.
  2. Direction test: if merge-tree reports conflicts, check git diff origin/main <branch> -- <file>. If the branch’s changes appear as removals, the branch is behind main, not ahead.
  3. Feature-presence test: confirm the feature exists on main (list the directory, search for the identifier) instead of trusting history.
  4. Ground truth for “was a PR merged”: gh pr list --state all --json headRefName,state.

To rebase a stale branch across the squash gap, use the end-state rule. For each conflicting file, if git diff origin/main <branch-tip> -- <file> is empty, taking main’s side at every intermediate rebase step cannot change the final tree, so it is safe to auto-resolve. Only files with a non-empty end-state diff need judgement; for those, search both trees for the APIs each side references (git grep): the branch is usually the stale side.