Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git best practice for config files etc

Tags:

git

github

config

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?

like image 773
Martin Hunt Avatar asked Feb 18 '12 16:02

Martin Hunt


People also ask

Should I commit config files?

Users should not commit app. conf into the repository, because different users may want different settings in that file.

Should I add project configuration files to Git?

The short answer to your question is Yes. I wouldn't hesitate to recommend Git (or any other version control software) to keep track of configuration files.

What files should not be stored in git?

You shouldn't store credentials like usernames, passwords, API keys and API secrets. If someone else steals your credentials, they can do nasty things with it.


1 Answers

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.)

like image 166
Alan W. Smith Avatar answered Sep 28 '22 03:09

Alan W. Smith