Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

config - Using conditional configuration files with Git

I frequently find myself changing a single variable in my project within Git to connect to a different server while on the Development branch (in JavaScript, so I can't use preprocessor defines).

Is there some way in Git that I can conditionally use one file or another depending on what branch I'm on?

I can't just commit that change with the different URL specified as doing so would leave it in my commit history, and if I go back to that version later on Master after it has been merged, it will have the development server URL.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...