Guide · Worktrees
Set up a clone or worktree without .env
A fresh clone or a new git worktree has no .env, because the file was never committed. Copying it from another folder spreads your keys one folder further.
In shortRun lk project in the new checkout to see which project it is, then either write an env file of references with lk export shop/development --out .env.local or skip the file and start the app with lk run --env shop/development -- npm run dev.
Why the file is missing
An env file is gitignored, so it stays in the folder where you made it. git worktree add and git clone give you the tracked files only. The usual fix is to copy the old file across, and every copy is another place a key can leak from.
Latchkey keeps a record for each environment: its variables, each pointing at a stored item or holding a plain setting such as PORT=3000. The record belongs to the project, not to a folder, so a new checkout can use it. The examples below use an example project called shop.
Set up the new checkout
-
Ask which project this is
Terminal cd ~/Projects/_worktrees/shop/fix-checkout lk projectLatchkey matches the checkout to a project by its Git remotes, read from git's config and never with credentials. It shows the matching project, its environments, and a suggested
exportcommand. -
Link the checkout if nothing matches
Terminal lk project link shopThis remembers the checkout's
originas projectshopand asks you first. Use--remote Rto name another remote. After that, a clone in any folder resolves toshop, and an import from it names items after the project rather than the folder. -
Write an env file of references
Terminal lk export shop/development --out .env.localThe file holds
latchkey://references and plain settings, sorted by name, and no values. If the file already exists, it is left alone and the variables that would change are named. Merge into an existing .env file covers that case..env.local, after # Latchkey environment shop/development DATABASE_URL=latchkey://shop/development/DATABASE_URL PORT=3000 STRIPE_KEY=latchkey://shop/development/STRIPE_KEY -
Or run without a file
Terminal lk run --env shop/development -- npm run dev--envsets the environment's variables from its record when the command starts. Nothing is written to the folder. Without a record for that environment, it loads what the older lookup finds and says so.
Let an agent work in the worktree
If a coding agent works in the new worktree, a grant for the main repository's folder covers it. Grants cover the folder, the folders inside it and that repository's worktrees.
lk grant 'shop/development/*' --folder ~/Projects/shop --for 2hThe grant asks once, lasts two hours here; --for always makes one that lasts until you revoke it. See Give coding agents API keys safely.
What this doesn't do
- A project match only names the project. It never releases a value or widens a grant.
- A match through a remote other than
origin, as a fork has, is flagged and never taken as the project. - Values are still released only after approval, unless an item is agent-safe or a grant covers it.
- A file written without
--valuesholds references, not values, so it needs Latchkey to fill them in when a command starts. - If an environment has no record yet,
lk env migratebuilds one from what Latchkey already knows. Variables found through old file locations stay unconfirmed until you runlk env confirm.
See the environments and export reference.