Give every git worktree its own database.
You use git worktree to work on several branches at once. But every worktree
points at the same local dev database. The moment one branch runs a migration,
it changes the schema the others rely on — so concurrent branches with diverging
migrations step on each other.
A fresh worktree is also missing everything git ignores: your .env / .dev.vars,
your node_modules. So it isn't even runnable until you copy things across by hand.
When you create a worktree, wtdb automatically:
- Copies your dev database into a new one named after the worktree
(Postgres, via a near-instant
CREATE DATABASE ... TEMPLATE). - Copies your env file into the worktree and rewrites the database URL to point at the new database.
- Copies the paths you list (e.g.
node_modules) using fast copy-on-write clones.
curl -fsSL https://raw.githubusercontent.com/willhackett/wtdb/main/install.sh | sh(Homebrew coming later, maybe.)
Add a .wtdb.json to the repo root:
{
"envFile": ".dev.vars",
"dbEnvVar": "DATABASE_URL",
"pathsToCopy": ["node_modules"]
}envFile— the env file to copy and rewrite (.env,.dev.vars, …).dbEnvVar— the variable in that file holding yourpostgres://…connection URL.pathsToCopy— gitignored dirs/files to bring into each new worktree.
Then install the git hook (once per repo):
wtdb init # writes a starter .wtdb.json if you don't have one
wtdb install # installs the post-checkout git hookJust use git as normal:
git worktree add ../myapp-feature
# -> wtdb copies the db, writes the env file, clones node_modulesWhen you remove a worktree, reclaim its database:
git worktree remove ../myapp-feature
wtdb prune # drops databases whose worktrees are goneOther commands:
wtdb sync # provision the current worktree manually
wtdb status # list worktrees and their databaseswtdb ships with a PostgreSQL adapter. The database layer is an interface
(internal/db), so support for other databases can be added by implementing
Copy / Drop for a connection-URL scheme and registering it. PRs welcome.
MIT