Guide · Merge

Merge into an existing .env file

You have a .env with your own comments and settings, and you want to add the project's variables to it. Writing a fresh file would throw all of that away.

In shortRun lk export shop/development --out .env.local --merge. Latchkey lists the names it will add and update, asks to go ahead, and leaves every other byte of the file as it is. --replace rewrites the whole file instead.

Why a plain export is careful

An env file collects things over time: notes to yourself, a variable you added for one experiment, blank lines that group sections. A tool that regenerates the file removes all of it. So lk export does not overwrite an existing file. It leaves the file alone and names the variables that would change.

The examples use an example project called shop.

Merge the variables

  1. See what would change

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

    With the file already there and no flag, nothing is written. Latchkey names the variables that would change, so you can decide before anything moves.

  2. Merge

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

    It updates the variables the environment's record has, appends the ones the file is missing, and lists the names it will add and update. Then it asks to go ahead. Given this file:

    .env.local, before
    # Stripe, test mode
    STRIPE_KEY=example-only
    
    # my own notes
    DEBUG_SQL=1

    the result keeps the comments, the blank line, the order and DEBUG_SQL:

    .env.local, after
    # Stripe, test mode
    STRIPE_KEY=latchkey://shop/development/STRIPE_KEY
    
    # my own notes
    DEBUG_SQL=1
    DATABASE_URL=latchkey://shop/development/DATABASE_URL

    A variable that already holds the right value is left as written. An updated line keeps its indentation and any export prefix, but loses an inline comment. Line endings and a last line without a newline are kept. If there is nothing to change, nothing is written.

  3. Skip the question in a script

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

    --yes skips the confirmation. Without a terminal it is required.

  4. Or replace the file

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

    --replace rewrites the whole file and loses its comments and other variables. Use it when you want the file to match the record exactly.

In the app

Choose Edit › Export Environment… on an item, or right-click a project and choose Export Environment. Pick references or values and a file. If the file exists, the sheet offers Merge, which lists the names it adds and updates before you press it, or Replace the file. It runs the same checks as the command.

Merging values

With --values the file gets real values, in plain text. That asks for approval every time, naming the file, and refuses a file inside a Git checkout unless it is ignored and untracked. A variable already in the file is listed as an update before its value is known, and a merged file becomes readable only by you. Paste env vars into a dashboard is the option that avoids a file.

What this doesn't do

The export reference lists every flag. For new clones, see Set up a clone or worktree without .env.

Related