Using conditional configuration files with Git

No, there isn’t, but this is a well-solved problem.

You have a few options:

Version control an example config file

  • Don’t store environment-specific data in version control
  • Create a config.example file which lists all the configuration options that need to be specified, and provides sane defaults for development.
  • Users who clone your repo should copy config.example to the real config filename, and add real values
  • Add the real config file’s name to .gitignore.
  • Store your production credentials outside of git, but backed up,
  • As a bonus, you can add a setup.sh script, which copies config.example to the real config’s location, and populates it with variables for the local environment

As an example, you might have a JavaScript application which needs to know where its database is, and reads this information from config/database.json. You might use something like this:

// config/database.example.json
DATABASE = {
  "host": "localhost",
  "user": "#TODO",
  "pass": "#TODO",
}

To get running in development, you would copy this file to config/database.json, and fill in the values appropriate to your dev environment.

In production, you’d have a config/database.json that contained production values, but was not version controlled.

The repo would have config/database.json in its .gitignore.

Version control environment-specific config files

  • Store X different configuration files, one per environment, call them config.development and config.production etc
  • Symlink the correct one for your environment.
  • add the symlink to .gitignore

If there is anything remotely sensitive in your config file, such as AWS keys or any form of password, you should use the first option – store the configuration option’s name, but not its value, and require users to supply their own credentials, obtained through secure channels outside of version control.

Leave a Comment