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

  1. Ask which project this is

    Terminal
    cd ~/Projects/_worktrees/shop/fix-checkout
    lk project

    Latchkey 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 export command.

  2. Link the checkout if nothing matches

    Terminal
    lk project link shop

    This remembers the checkout's origin as project shop and asks you first. Use --remote R to name another remote. After that, a clone in any folder resolves to shop, and an import from it names items after the project rather than the folder.

  3. Write an env file of references

    Terminal
    lk export shop/development --out .env.local

    The 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
  4. Or run without a file

    Terminal
    lk run --env shop/development -- npm run dev

    --env sets 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.

Terminal
lk grant 'shop/development/*' --folder ~/Projects/shop --for 2h

The 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

See the environments and export reference.

Related