Tuesday, November 13, 2018

How to keep different configuration files for different release/development/testing environment using Git

Very often you need different configuration files for different cases. For example, you may need to connect to different database servers for production/development versions. Suppose you need different persistence.xml files for it.

You can use Merge Strategies using Git Attributes.

Git attributes are set either in a .gitattributes file in one of your directories (normally the root of your project) or in the .git/info/attributes file if you don’t want the attributes file committed with your project.

You can use Git attributes to tell Git to use different merge strategies for specific files in your project. One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s.

This is helpful if a branch in your project has diverged or is specialized, but you want to be able to merge changes back in from it, and you want to ignore certain files. Say you have a database settings file called persistence.xml that is different in two branches, and you want to merge in your other branch without messing up the database file. You can set up an attribute like this:

persistence.xml merge=ours

If you merge in the other branch, instead of having merge conflicts with the persistence.xml file, you see something like this:

$ git merge topic
Auto-merging persistence.xml
Merge made by recursive.

In this case, persistence.xml stays at whatever version you originally had.

For more information, check Customizing Git using Git Attributes