Sub topic 8
Dependencies, Ports and Untracked Files
This chapter assumes you can create worktrees with git worktree add, run a command-line coding agent, and use background processes in a shell. It covers what a new worktree does not contain, and what two agents running side by side can still fight over.
What a new worktree is missing
A worktree is a checkout of tracked files. Git populates it from the commit you chose and nothing else. Anything that lives in your main directory but is not committed stays behind. That usually includes:
- Dependency directories such as
node_modules,.venvorvendor. - Local configuration such as
.env,.env.localor credentials files. - Build output and caches such as
dist,targetor.next. - Local databases, such as a SQLite file created during development.
Most of these are listed in .gitignore, which is why they are not in the new directory. An agent started in a fresh worktree will see a project that cannot build, cannot run tests and cannot connect to anything. Agents often react by trying to fix this themselves, which wastes time and sometimes produces odd changes. It is better to prepare the worktree before the agent starts.
You can see what is missing by listing the files Git ignores in the main checkout:
git status --ignored --shortLines that start with !! are ignored files. Decide for each one whether the new worktree needs a copy, a fresh install, or nothing.
Dependencies: install, copy or share
You have three options for dependency directories. Each trades disk space and time against safety.
| Approach | How | Cost | Risk |
|---|---|---|---|
| Install per worktree | Run the package manager in each worktree | Slowest, most disk | Lowest. Each agent has a private copy |
| Copy | Copy the directory from the main checkout | Fast, uses disk | Low, but native binaries and absolute paths can break |
| Symlink | Point the worktree's directory at the main one | Fastest, no extra disk | High. One agent adding a package changes all of them |
Installing per worktree is the default choice. Package managers with a global cache make it cheaper than it sounds, because they link files from the cache instead of downloading them again.
cd ../myrepo-auth
npm ciUse npm ci rather than npm install. It installs exactly what the lockfile says and does not rewrite it, so the agent starts from a known state.
For Python, create the virtual environment inside the worktree so that it is tied to that directory:
cd ../myrepo-auth
python -m venv .venv
.venv/bin/pip install -r requirements.txtAvoid copying a virtual environment. A venv stores absolute paths to its own location, and a copy in another directory will still point at the original.
Symlinking is only safe when the agents are told not to change dependencies. If one task needs a new package, that task must have its own install.
Untracked files: copy the configuration
Configuration files such as .env hold secrets and local settings, so they are deliberately not committed. Copy them into each worktree as part of setup:
for task in auth billing search; do
cp ../myrepo/.env "../myrepo-$task/.env"
doneCheck first that the file is ignored in the worktree too. The .gitignore is tracked, so the new worktree has it, and the copied .env will not show up as a change. Confirm it:
cd ../myrepo-auth
git check-ignore -v .envIf this prints nothing, .env is not ignored, and an agent running git add . could commit your secrets.
Give each agent only the secrets its task needs. A .env copied from your main checkout may hold production keys. For agent work, create a separate .env.agent that holds development or test credentials, and copy that file instead.
Ports: the collision that worktrees cannot prevent
Two directories can hold two copies of a dev server, but the machine has one set of network ports. If every worktree's .env says PORT=3000, the first server starts and the others fail with an "address already in use" error. Agents that see this error sometimes kill the process holding the port, and that process may belong to another agent.
The fix is to assign each worktree its own port range when you set it up. A simple scheme is a base port plus an index:
#!/usr/bin/env bash
set -euo pipefail
index=0
for task in auth billing search; do
dir="../myrepo-$task"
app_port=$((3000 + index * 10))
db_port=$((5432 + index + 1))
cp ../myrepo/.env.agent "$dir/.env"
{
echo "PORT=$app_port"
echo "DB_PORT=$db_port"
} >> "$dir/.env"
index=$((index + 1))
doneThis appends the port lines to the end of each copied file. Most .env loaders take the last value when a key appears twice, but check this for yours. If the loader takes the first value, remove the key from .env.agent instead of appending.
Spacing the application ports by ten leaves room for extra services, such as a test server or a debugger, without overlapping the next worktree.
The application must read the port from the environment. If a port is hard-coded in a tracked file, no .env can change it. In that case, fix the code first, or run that part of the project in one worktree at a time.
Databases and other shared services
A port number is only one form of shared resource. If all worktrees use the same local database, then migrations, test fixtures and deletes from one agent affect the rest. Options, from simplest to strongest:
- Give each worktree its own database name, such as
myapp_authandmyapp_billing, on the same server. - Use a file-based database with a path inside the worktree, so each copy is separate.
- Start a separate database container per worktree on its own port.
Test suites are the usual cause of trouble. A test run that drops and recreates tables will break an agent running in a different worktree against the same database. Name the test database per worktree as well.
The same rule applies to other resources outside the directory: caches in your home directory, message queues, temporary file paths and lock files. If two agents write to the same fixed path, they race.
Build output and caches
Build tools write to directories inside the project, which is safe, because each worktree has its own. Problems appear when a tool keeps a cache in a shared location, such as a global build cache under your home directory. Shared caches are usually designed for concurrent use, so this tends to be fine. Treat a cache as suspect if you see strange failures that disappear when you run one agent at a time.
A fresh worktree also has no build output, so the first build is a full build. Plan for that when you judge how long an agent will take.
A worktree setup checklist
The quick check in step five can be as small as running the test suite once, or starting the server and requesting one page. A failure here is far cheaper to find before the agent starts than after it has spent several minutes working around the problem.
cd ../myrepo-auth
npm test -- --runPut the steps in a script so that every worktree is prepared the same way. Chapter 13 of this tutorial builds that script out in full.
Common errors
- The agent reports missing modules. The dependencies were never installed in that worktree. Install them before starting the agent.
- "Address already in use." Two worktrees share a port. Assign distinct ports and make sure the code reads them from the environment.
- Tests pass in one worktree and fail in another. Look for a shared database, a fixed temp path, or a stale cache.
- A secret appears in
git status. The file is not ignored. Add it to.gitignorein a commit onmainbefore creating further worktrees. - A copied virtual environment runs the wrong code. It still points at the original directory. Recreate it in place.
Exercise
Three worktrees each need a dev server and a Postgres database. Describe what you would put in each worktree's .env so the agents cannot interfere, and name one thing that would still be shared even after you do this.
Show answer
Give each worktree its own application port (for example 3000, 3010, 3020) and its own database port or database name (for example myapp_auth, myapp_billing, myapp_search). Use test credentials rather than production keys. Still shared: the machine's other resources, such as the same Postgres server if the databases live on one instance (so heavy load or server-level settings affect everyone), global caches under the home directory, and the underlying Git repository's refs and configuration.
Exercise
You copy your main checkout's .venv into a new worktree and pip install a new package from there. Which environment changes, and why is copying a virtual environment a poor choice?
Show answer
The copied .venv still holds absolute paths to the original location, so its scripts and interpreter links can point at the main checkout's environment, and the install may modify the original rather than the copy. Create a new virtual environment inside the worktree instead.