Git Worktrees Don't Isolate Your Database or Ports
Parallel agents in git worktrees still share a dev database and port 3000. A reproducible test, a per-worktree .env.local fix, and cleanup that deletes the local DB.
- A git worktree gives each agent its own files and branch, but a database path or port set outside the repository is still shared.
- In our test, a reset in one worktree emptied the other worktree's data, and the second dev server failed with EADDRINUSE on port 3000.
- Give each worktree its own gitignored .env.local with a separate PORT and a database path inside the worktree, then check that both run side by side.
Git worktrees keep parallel agents from editing the same files, but they don’t separate anything that lives outside the checkout. A dev database at a fixed path and the default dev-server port are still shared. This article reproduces both problems in a throwaway repository, fixes them with a per-worktree settings file, and checks what cleanup does to the local database. For worktree basics and Claude Code’s --worktree flag, see Run Parallel AI Agents with Git Worktrees.
What a worktree separates and what it doesn’t
A worktree has its own working directory, its own checked-out branch and its own index. The repository history is shared. Anything your app finds through a fixed path or a fixed number is not part of the checkout, so worktrees don’t change it:
- a database file or server set by an absolute path or a shared connection string
- the default port the dev server listens on
- caches or uploads written to a fixed folder outside the repository
Source: https://git-scm.com/docs/git-worktree (checked 2026-11-09)
The test setup
We ran this on 2026-11-09 in the editorial team’s test environment, in a new repository made with git init, with no remote, and re-ran the same steps on 2026-11-11 with the same results. We deleted the repository afterwards.
git version 2.47.3
node v24.22.1
The app is one small Node.js file. It reads PORT (default 3000) and DATABASE_PATH (default an absolute path outside the repository), and uses the built-in node:sqlite module. It can add a row (seed), delete all rows (reset), print the row count (count), or start an HTTP server.
const port = Number(process.env.PORT ?? 3000);
const dbPath = process.env.DATABASE_PATH ?? '/tmp/agentkoji-worktree-test/shared/dev.db';
We committed it, ignored .data/ and .env.local in .gitignore, and made two worktrees, one per agent:
git worktree add ../wt-a -b agent-a
git worktree add ../wt-b -b agent-b
/tmp/agentkoji-worktree-test/todo-app 71526d3 [main]
/tmp/agentkoji-worktree-test/wt-a 71526d3 [agent-a]
/tmp/agentkoji-worktree-test/wt-b 71526d3 [agent-b]
Shared database: one reset empties both
With default settings, we added a row in wt-a and counted in wt-b. Then we reset in wt-b and counted in wt-a:
$ (wt-a) node app.mjs seed from-a
$ (wt-b) node app.mjs count
/tmp/agentkoji-worktree-test/shared/dev.db todos=1
$ (wt-b) node app.mjs reset
$ (wt-a) node app.mjs count
/tmp/agentkoji-worktree-test/shared/dev.db todos=0
Agent B saw agent A’s data, and agent B’s reset deleted it. If one agent runs migrations or test fixtures while the other is mid-task, the other agent’s results can change without any file in its worktree changing.
Shared port: the second server fails
We started the server in wt-a, then in wt-b:
$ (wt-a) node app.mjs
listening 3000 db=/tmp/agentkoji-worktree-test/shared/dev.db
$ (wt-b) node app.mjs
EADDRINUSE port 3000
Our app prints the error code and exits. Dev servers handle this differently. Some fail like this, and some move to another port on their own, so check which port each agent’s server actually uses.
Give each worktree its own settings
We gave each worktree a gitignored .env.local with its own port and a database path inside the worktree:
# wt-a/.env.local
PORT=3101
DATABASE_PATH=./.data/dev.db
# wt-b/.env.local
PORT=3102
DATABASE_PATH=./.data/dev.db
Node.js loads it with --env-file. Source: https://nodejs.org/api/cli.html#—env-filefile (checked 2026-11-09)
We ran the same steps again: a row added in wt-a, a row added and then reset in wt-b:
$ (wt-a) node --env-file=.env.local app.mjs count
./.data/dev.db todos=1
$ (wt-b) node --env-file=.env.local app.mjs count
./.data/dev.db todos=0
Both servers started, and both answered:
listening 3101 db=./.data/dev.db
listening 3102 db=./.data/dev.db
$ curl -s 127.0.0.1:3101
ok 3101
$ curl -s 127.0.0.1:3102
ok 3102
A relative path like ./.data/dev.db resolves from the directory you start the app in, so start each agent from its worktree root. If your project uses a database server instead of a file, give each worktree its own database name in the connection string.
Because .env.local is gitignored, a new worktree doesn’t have it. Create it when you add the worktree. Claude Code can copy gitignored files into the worktrees it creates (--worktree, subagent worktrees and desktop-app parallel sessions): list them in a .worktreeinclude file at the project root, in .gitignore syntax. Only files that match a pattern and are also gitignored are copied. Note what that means here: it copies the main checkout’s .env.local as-is, so every worktree would get the same PORT and you’d be back to the port clash. Copy a template instead, or change PORT after the worktree is created. We didn’t test .worktreeinclude ourselves (not run). Source: https://code.claude.com/docs/en/worktrees (checked 2026-11-14)
Cleanup deletes the local database
Git counts ignored files differently from untracked ones. git status --ignored showed only ignored files in wt-a:
!! .data/
!! .env.local
The git docs say only clean worktrees, with no untracked files and no changes to tracked files, can be removed without --force. Ignored files don’t count, so git worktree remove succeeded for both worktrees, and the .data/ folder with its database went with them:
$ git worktree remove ../wt-a && echo removed-a
removed-a
$ git worktree remove ../wt-b && echo removed-b
removed-b
$ git worktree list
/tmp/agentkoji-worktree-test/todo-app 71526d3 [main]
Source: https://git-scm.com/docs/git-worktree (checked 2026-11-09)
If an agent’s local data needs to survive, export it or keep it outside the worktree before you remove it.
Checklist before starting parallel agents
- Find every setting with a fixed path, port or connection string, including defaults in code.
- Give each worktree its own gitignored
.env.localwith a separatePORTand database path or name. - Start one server per worktree and confirm each answers on its own port.
- Add a row in one worktree and confirm the other doesn’t see it.
- Before
git worktree remove, save any local data you still need.
Related reading
- Run Parallel AI Agents with Git Worktrees: worktree basics,
claude --worktreeand cleanup. - Subagent Failures in Parallel Runs: what went wrong when subagents worked in separate worktrees.
- How to Define Claude Code Subagents: subagent files, tools and
isolation: worktree. - Claude Code Subagents and Parallel Agents: our hub for subagents, agent teams and parallel sessions.
Summary
- Worktrees separate files and branches, not databases or ports set outside the checkout.
- In our test, one worktree’s reset emptied the other’s data, and the second server failed with
EADDRINUSEon port 3000. - A per-worktree
.env.localwith its ownPORTandDATABASE_PATHlet both run side by side with separate data. git worktree removedoesn’t treat ignored files as untracked, so it deleted the local database without--force.
FAQ
- Does git worktree create a separate database for each worktree?
- No. A worktree is a separate checkout of the files. If the app's database path points outside the repository, every worktree uses the same database. In our test, a reset in one worktree emptied the data the other worktree had added.
- Why does the second dev server fail with EADDRINUSE?
- Both worktrees started the same app with the same default port, 3000, so the second server could not bind it. Setting a different PORT in each worktree's .env.local let both run.
- Does git worktree remove delete a local database stored in the worktree?
- Yes, in our test. The database lived in a gitignored .data/ folder, git worktree remove succeeded without --force, and the folder was deleted with the worktree. Keep anything you need outside the worktree.