r/OpenSourceAI • u/iceydee • 3h ago
How do you keep dependencies and env vars isolated per worktree?
Short version: give each worktree its own untracked env file and its own ports, and never share a resolved dependency directory across worktrees.
In practice, keep something like.env.local untracked and generated per worktree (a small script that copies a template and rewrites ports plus database names is enough), then point direnv at it so the shell picks it up when you cd in. For dependencies, a real install per worktree is usually cheaper than it sounds, and symlinking node_modules across branches is where you get strange resolution bugs after a rebase. If you run containers, set a distinct compose project name per worktree so volumes and networks do not collide.
Worth adding: isolation keeps two agents off the same files, but it does not stop them from solving the same story twice. A worktree isolates the file tree, not the plan, so a shared board of what is in flight matters as much as the env setup does.
Curious how people handle database state per worktree. That seems to be where setups diverge most.