sync command analyzes all .env* files in your project directory and ensures they all contain the same environment variable keys, while preserving existing values.
Usage
What It Does
The sync command performs the following operations:- Discovers all
.env*files in the current directory (.env,.env.local,.env.development,.env.testing,.env.staging,.env.example) - Analyzes differences between files to identify missing keys
- Determines a “Source of Truth” file (either specified or automatically detected)
- Interactively resolves missing keys by asking how to handle them:
- Add empty value
- Copy value from another file
- Skip the key
- Ensures
.env.examplecontains all keys with placeholder values - Creates a backup in
.env-twin/before making any changes - Updates files while preserving existing values and structure
The sync command always creates a backup before modifying files unless you use the
--no-backup flag.Options
Specify the “Source of Truth” file - keys will be synced FROM this file to all othersAliases:
--srcIf not specified, env-twin will detect the most appropriate source of truth based on:- File with the most keys
- Alphabetical precedence (e.g.,
.envbefore.env.local)
Skip creating a backup before syncingExample:
Skip confirmation prompts and auto-accept non-destructive actionsAliases:
-yThis flag will automatically accept safe operations but still prompt for potentially destructive actions.Example:Output the analysis report in JSON format (useful for programmatic access or AI tools)When this flag is used, the command outputs a structured JSON report and exits without performing any sync operations.Example:Output structure:
Display help information for the sync commandAliases:
-hExample:Examples
Basic sync operation
Basic sync operation
Synchronize all .env* files in the current directory:Output:
Specify source of truth
Specify source of truth
Use This ensures that all files contain the keys defined in
.env.example as the authoritative source:.env.example, which is useful when:.env.exampleis your documentation- You want to ensure completeness across environments
- You’re onboarding new developers
Auto-accept with --yes flag
Auto-accept with --yes flag
Skip confirmation prompts for faster execution:The
--yes flag automatically accepts:- Creating backups
- Adding empty values for missing keys
- Non-destructive operations
Skip backup (dangerous)
Skip backup (dangerous)
Sync without creating a backup:
Get JSON output for analysis
Get JSON output for analysis
Output analysis data in JSON format:Example output:This is useful for:
- CI/CD pipelines
- Automated reporting
- Integration with other tools
Combine multiple options
Combine multiple options
Use multiple flags together:This will:
- Use
.env.exampleas the source of truth - Auto-accept safe operations
- Create a backup (default behavior)
Interactive Resolution
When the sync command detects missing keys, it provides interactive prompts to resolve them:Resolution Options
- Add Empty Value: Adds the key with an empty string value (
KEY="") - Copy Value: Copies the value from another file (e.g., from source of truth)
- Skip: Leaves the file unchanged for this key
Example Interactive Session
Backup Location
Backups are stored in the.env-twin/ directory with timestamps:
YYYYMMDD-HHMMSS.
To restore from a backup, use the
restore command.Supported .env Files
The sync command automatically detects these file patterns:.env.env.local.env.development.env.testing.env.staging.env.production.env.example- Any other
.env.*files
Best Practices
Use .env.example as documentation
Use .env.example as documentation
Set This ensures developers know what variables they need to configure.
.env.example as your source of truth to document all required environment variables:Sync before committing
Sync before committing
Run sync before committing to ensure all environment files are in sync:
Keep backups for safety
Keep backups for safety
Always keep backups enabled (don’t use
--no-backup) in production environments.Use clean-backups to manage old backups:Review JSON output in CI/CD
Review JSON output in CI/CD
Use
--json in CI/CD pipelines to validate environment file consistency:Related Commands
restore
Restore .env files from backups created by sync
clean-backups
Clean up old backups to save disk space
Troubleshooting
No .env* files found
No .env* files found
Problem: Command reports “No .env* files found”Solution: Make sure you’re in the correct directory and have at least one
.env file. Create one if needed:Permission denied errors
Permission denied errors
Problem: Cannot read or write .env filesSolution: Check file permissions:
Backup creation fails
Backup creation fails
Problem: Cannot create backup in .env-twin/Solution: Ensure the directory is writable or use
--no-backup (not recommended):