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
-
See what would change
Terminal lk export shop/development --out .env.localWith the file already there and no flag, nothing is written. Latchkey names the variables that would change, so you can decide before anything moves.
-
Merge
Terminal lk export shop/development --out .env.local --mergeIt updates the variables the environment's record has, appends the ones the file is missing, and lists the names it will
addandupdate. Then it asks to go ahead. Given this file:.env.local, before # Stripe, test mode STRIPE_KEY=example-only # my own notes DEBUG_SQL=1the 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_URLA variable that already holds the right value is left as written. An updated line keeps its indentation and any
exportprefix, 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. -
Skip the question in a script
Terminal lk export shop/development --out .env.local --merge --yes--yesskips the confirmation. Without a terminal it is required. -
Or replace the file
Terminal lk export shop/development --out .env.local --replace--replacerewrites 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 file must be UTF-8
KEY=VALUElines. - It never removes a variable. Ones Latchkey does not manage stay, including ones you no longer want.
- If the file changes while the question waits, the merge is refused. Run it again.
- A symbolic link, or anything but a regular file, at the destination is refused.
- A file that does not exist yet is created, as without
--merge.
The export reference lists every flag. For new clones, see Set up a clone or worktree without .env.