Keep config files untouched in Git branches

In this post we’ll consider rather common situation when you’ve got application with dedicated configuration file and at least two environments: dev and prod.
Application in dev env is using dev database and prod application is using prod database;
Database connection strings are stored in app.config file, so you have to have different app.config files in dev and prod Git branches.
The most common suggestion about managing this challenge is to use git attributes merging policy, so let’s see how to deal with it.

Create merge strategy which will use driver ours:

screenshot-at-dec-04-17-54-27

Create (I’ll use BitBucket) and clone our demo repo:

screenshot-at-dec-04-17-55-11

Now we can create and commit .gitattributes file on root level of the repo (use .git/info/attributes if you want to use it only in the local repo);
this file will set merge behavior for the app configuration file which happens to be app.config in our example:

screenshot-at-dec-04-17-56-31

Well, it’s time to create dev branch, add our application files and configuration file:

screenshot-at-dec-04-17-58-49

Ok, our dev app is fine, so we can create prod branch from dev:

screenshot-at-dec-04-18-00-01

First of all we want to change database connection string:

screenshot-at-dec-04-18-01-32

Perfect, our app is running correctly in both dev and prod environments now,
But what if some changes were added to app in dev and now they need to be merged to prod?
Let’s take a look at this:

screenshot-at-dec-04-18-04-07

As you can see, application code was successfully updated and configuration files are still different for dev and prod:

screenshot-at-dec-04-18-04-47

But what if app.config file has been changed on dev?

screenshot-at-dec-04-18-08-05

As you can see we’re still on the safe side – changes in dev config don’t affect our prod configurations.
You can ensure that if configuration files were changed in both dev and prod, they still will be merged correctly:

screenshot-at-dec-04-18-18-16

So what’s the problem with this scenario?
In real life you probably don’t want to do merges using CLI but do Pull requests;
The very pity thing is than considered merging policy¬†doesn’t support in Pull requests:

screenshot-at-dec-04-18-44-34

Situation is the same for BitBucket, GitHub and Visual Studio Team Services.
However, you can review pull request and keep in mind that you don’t have to care about certain files (app.config in our example),
When you finish Pull request review, perform merge using CLI and ensure that it has been completed successfully despite warnings in GUI:

screenshot-at-dec-04-19-00-13

screenshot-at-dec-04-19-01-21

As for me, this solution looks like:

bug-1200x705

So how the general challenge can be managed?
I suggest using two different files – app.config.dev and app.config.prod ;
During CD suitable file will be copied and renamed.
You can also consider using OS env variables or parsing config with tools like AWK.

VCS table of contents