Git best practice for config files etc


I’m still new to all things git and was wondering what is best practice in regards to config files. My local development server needs different config values to my live one so how can I stop it pushing / pulling those files?


Use symbolic links.

Take an example where you have a config file named “config.ini”. In the working directory of your git repo, you would do the following:

  1. Create a version of the config file called “config-sample.ini”. This is the file you’ll do all your work on.

  2. Create a symbolic link between “config.ini” and “config-sample.ini”.

    ln -s config-sample.ini config.ini

    This let’s all your code point to “config.ini” even though you’re really maintaining “config-sample.ini”.

  3. Update your .gitignore to prevent the “config.ini” from being stored. That is, add a “config.ini” line:

    echo "config.ini" >> .gitignore
  4. (Optional, but highly recommended) Create a .gitattributes file with the line “config.ini export-ignore”.

    echo "config.ini export-ignore" >> .gitattributes
  5. Do some coding and deploy….

  6. After deploying your code to production, copy the “config-sample.ini” file over to “config.ini”. You’ll need to make any adjustments necessary to setup for production. You’ll only need to do this the first time you deploy and any time you change the structure of your config file.

A few benefits of this:

  • The structure of your config file is maintained in the repo.

  • Reasonable defaults can be maintained for any config options that are the same between dev and production.

  • Your “config-sample.ini” will update whenever you push a new version to production. This makes it a lot easier to spot any changes you need to make in your “config.ini” file.

  • You will never overwrite the production version of “config.ini”. (The optional step 4 with the .gitattributes file adds an extra guarantee that you’ll never export your “config.ini” file even if you accidentally add it to the repo.)

(This works great for me on Mac and Linux. I’m guessing there is a corresponding solution possible on Windows, but someone else will have to comment on that.)

Answered By – Alan W. Smith

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published