Sub topic 3
What Worktrees Do Not Isolate
Assume you know everyday Git commands, basic shell usage, and how to run a coding agent. Git worktrees separate working directories and indexes, but they do not create separate machines or repositories.
Shared state that can still collide
All worktrees usually share the repository’s Git object database, references, and local configuration. A commit created in one worktree becomes available to the others. A branch can still be checked out in only one worktree at a time.
Worktrees also share resources outside Git:
- Database servers, queues, and caches
- Local service ports
- Container names and volumes
- Dependency caches
- Repository-level hooks and configuration
- Secrets or configuration copied into more than one worktree
Give each agent distinct ports and test databases. Copy local configuration deliberately. An untracked .env file is separate in each worktree, but copying the same values can still make agents connect to the same external service.
Review and merge branches
Treat every agent branch as a proposed change. Inspect its three-dot diff against the integration branch, run its tests, then merge it:
git diff main...agent/auth
git switch main
git merge --no-ff agent/authMerge one branch at a time. This keeps failures attributable and exposes conflicts early.
Resolve conflicts
If two agents edit overlapping lines, Git stops the merge. Open each conflicted file, choose or combine the correct changes, then mark the file resolved:
git status
# edit the files containing conflict markers
git add path/to/file
git commitRun the relevant tests after resolving. If the changes are not ready, abort with git merge --abort.
Clean up safely
Remove a clean worktree, then delete its merged branch:
git worktree remove ../myrepo-auth
git branch -d agent/auth
git worktree prunegit worktree remove refuses to discard uncommitted work. Preserve or explicitly review those changes before using force options.
Script the lifecycle
#!/usr/bin/env bash
set -euo pipefail
repo=$(git rev-parse --show-toplevel)
name=$(basename "$repo")
action=${1:-setup}
shift || true
tasks=("${@:-auth billing search}")
if [[ "$action" == setup ]]; then
for task in "${tasks[@]}"; do
git worktree add -b "agent/$task" "../$name-$task" main
done
elif [[ "$action" == clean ]]; then
for task in "${tasks[@]}"; do
git worktree remove "../$name-$task"
git branch -d "agent/$task"
done
git worktree prune
else
printf 'usage: %s {setup|clean} [task ...]\n' "$0" >&2
exit 2
fiLimits and alternatives
Worktrees are efficient when agents share one repository and can use separate branches. Use containers when dependencies, ports, operating-system packages, or databases must be isolated. Use separate clones when repository metadata, credentials, or Git configuration must be independent. These methods cost more disk space and setup time, but reduce shared-state failures.
Exercise
An agent needs a different database version and must not access your local database. Are worktrees alone sufficient?
Show answer
No. Use a container or another isolated environment, and give the agent its own database and configuration.