06 / 10Builder46 min lesson + lab

Work Across Environments and Worktrees

Keep source, runtime, preview, production, local work, cloud work, branches, and parallel checkouts separate enough that every change lands where you intend.

By the end

An environment map, an isolated Git worktree workflow, and an integration procedure that prevents concurrent work from colliding or being mistaken for deployment.

01

Separate the proof layers

A modern project may exist as source in Git, one or more local checkouts, a running development process, a generated build, a shareable preview, an installed application, and a production deployment. A customer may be using yet another persisted version. These layers can drift. A change visible in one says nothing automatic about the others.

Name the layer in every task and receipt. 'The source contains the fix' is a source claim. 'The local page works' is a runtime claim. 'Version 123 is receiving production traffic' is a deployment claim. This vocabulary prevents a large class of false completion reports.

02

Understand the local environment

A local task works with files and tools on the user's machine, subject to its sandbox and permissions. It may rely on installed runtimes, local databases, environment variables, cached dependencies, or native applications. Record those dependencies rather than assuming every collaborator has the same setup.

Local convenience can hide production differences. A development server may use mock data, a permissive login, or a local port that does not exist after deployment. Verification must name which dependencies were real and which were substituted.

03

Understand the cloud environment

A cloud task runs in a hosted environment configured for that task. It can work independently of the laptop and is useful for isolated or longer-running work, but it does not automatically see local uncommitted files, local-only services, or credentials stored on the machine. Setup instructions must make required dependencies reproducible without exposing secrets.

Treat cloud output as a proposed change until it is reviewed and integrated into the intended branch. A successful hosted test proves the hosted setup and commit, not the state of a developer's local checkout or the production service.

04

Use worktrees for parallel change

A Git worktree is another working directory attached to the same repository history. It lets one task work on one branch while another task or human uses a different directory and branch. This is safer than having two agents edit the same files in one checkout and cheaper than repeatedly cloning the full repository.

In the desktop app, choose Worktree beneath the composer, select the starting branch, and submit the task. Codex creates a managed worktree, initially in detached HEAD state. Use Create branch here if the work should remain in that worktree, or use Hand off to Local when you want the chat and changes moved into the foreground checkout for your usual IDE or single-instance runtime. Do not try to check out the same branch in two worktrees; Git rejects it by design.

Isolation is not automatic coordination. Give each worktree a clear owner, file scope, and acceptance test. Shared external resources such as databases, ports, caches, or generated directories can still collide even when files do not. Assign separate test data or runtime ports where needed. If a managed worktree needs an ignored local setup file, `.worktreeinclude` can list it for copying, but every listed secret-bearing file widens exposure and must follow the project's credential policy.

05

Choose Handoff or a permanent branch deliberately

Stay in the managed worktree when its dependencies and tools are reproducible there. Use Open to inspect that worktree in an IDE, run its own development process, and choose Create branch here before committing or opening a pull request. Keep the branch attached to that worktree while it is active.

Use Hand off to Local when you need the foreground environment: a running service that can exist only once, a native simulator, a local-only dependency, or the exact IDE setup already open. Handoff performs the Git movement and preserves the chat. Before and after handoff, compare `git status --short`, the branch or detached state, and the changed file list so the transfer itself has a receipt.

06

Integrate with evidence

Before combining parallel work, compare each branch with its base, review every changed file, and rerun checks on the integrated result. Two branches can pass separately and fail together because they changed a shared contract, dependency, route, or schema.

Keep the original worktree until integration is proven or a clear recovery point exists. Removing an isolated checkout too early discards convenient evidence about what happened, even if the commits remain recoverable. Cleanup belongs after integration and receipts, not before.

Working reference

Commands and patterns

Inspect branches and worktrees

git status --short
git branch --show-current
git worktree list

Establish current changes, branch ownership, and every attached checkout before starting parallel work.

Create an isolated feature worktree

git worktree add ../project-feature -b feature/project-feature

Use an explicit validated sibling path and a descriptive new branch. Replace both example names with task-specific values.

Review integration scope

git diff --stat main...HEAD
git diff main...HEAD

Inspect the branch's complete change against the actual base branch before integration. Substitute the repository's real base branch.

Optional managed-worktree include file

# .worktreeinclude
.env.local
config/local-fixtures.json

Only list ignored files that a desktop-app managed worktree genuinely needs. Review their contents and access policy first; never commit credential values, and prefer reproducible setup over copying secrets.

When the happy path breaks

Failure modes

01

Symptom

Two agents overwrite or repeatedly undo each other's edits.

Likely cause

Both tasks are writing to the same checkout or overlapping files without explicit ownership.

Recovery

Stop concurrent writes, preserve both states, move independent work to separate branches or worktrees, and assign non-overlapping scopes before resuming.

02

Symptom

A cloud result is missing local work or fails against a local dependency.

Likely cause

The hosted environment was assumed to inherit uncommitted files, services, or machine credentials.

Recovery

Commit or otherwise supply the intended source, encode reproducible setup, and use approved secret configuration or choose the local environment when the dependency cannot be reproduced safely.

03

Symptom

Two isolated branches pass independently but the merged application fails.

Likely cause

The changes interact through a shared interface, schema, dependency, configuration, or runtime resource.

Recovery

Review combined diffs, integrate in a dedicated branch or checkout, and rerun the complete risk-matched verification stack on the combined state.

04

Symptom

A local or preview result is reported as production-ready or live.

Likely cause

The source, runtime, preview, and deployment proof layers were collapsed into one status.

Recovery

Correct the claim, identify the highest verified layer, and gather a versioned deployment receipt plus independent live-route evidence before calling it production.

Hands-on lab

Run two safe parallel changes

Use two worktrees to make independent changes, then prove the integrated result without losing either branch's evidence.

  1. 01Record the canonical repository, base branch, dirty state, current worktrees, and shared runtime resources with the Git inspection commands.
  2. 02In the desktop app, start one chat as Local and a second as Worktree from the same base. Give them independent file boundaries, runtime ports or test data, and acceptance checks.
  3. 03Complete the background task in its managed worktree. Use Open to inspect its directory and record detached or branch state, changed files, and verification output.
  4. 04Choose one path deliberately: Create branch here and keep working in the worktree, or Hand off to Local because a foreground-only dependency is required. Capture status before and after.
  5. 05Complete the second independent task without allowing either chat to edit the other's owned files.
  6. 06Compare both changes against the same base and identify shared contracts before integration. Rerun automated and user-flow checks on the combined state.
  7. 07Keep both original receipts until the combined checks are complete, then archive or clean up through the app only after confirming the recovery point.

Deliverable: A worktree ownership manifest, two independently verified branches, and a combined integration receipt.

Reusable artifact

Environment and worktree manifest

A lightweight contract that keeps parallel tasks and proof layers distinct.

Canonical repository and base branch: [repo + branch]
Task owner: [human or agent]
Worktree path: [absolute path]
Feature branch: [branch]
Allowed file scope: [paths]
Shared resources: [ports, database, cache, generated files]
Environment setup: [commands and variable names]
Proof layer: [source / local / cloud / preview / production]
Acceptance checks: [commands and flows]
Integration dependency: [other branches or contracts]
Recovery point: [commit, branch, version]
Cleanup only after: [receipts and review]

Before you move on

Receipt checklist

  • Each task has a distinct branch, worktree, owner, and allowed file scope.
  • Local-only and cloud-required dependencies are documented without secret values.
  • Each branch passes its own acceptance checks before integration.
  • The combined state is reviewed and tested after integration rather than inferred from separate success.
  • The final status names the highest actually verified environment layer.

Primary sources

Continue with the source

Home
Book
Blog
Calugaru Labs
GitHub
LinkedIn
Email
Theme