4

I have a web project with two git branches (developing and production) and each one connects to a different server (dev and prod). Each branch has a set of tracked config files, such as different URLs, database options, sql exported files, etc. which are different on both branches.

Every time I try to merge those branches I get a lot of conflicts in these settings files which need to be resolved manually, but every now and then I miss something. In short, it is a nightmare. My question is therefore, how can I avoid this?

  • Is there a way to prevent any changes to those config files while merging or rebasing?*

Suggested Solution: "Try to make those files untracked". I tried this but whenever I pulled from the production file I get those files deleted and replaced, so I don't know if I should do it in a different way.

user1978011
  • 3,419
  • 25
  • 38
VaTo
  • 2,936
  • 7
  • 38
  • 77

1 Answers1

2

The way to do this is to register a merge strategy as per-file attribute that will always use the current version. Here is how to do this (the idea is from this blog)

First you register a merge driver that

  • does nothing and
  • exits successfully.

The shell command true is exactly what we are looking for. The following line registers true as the alwaysours merge driver:

git config --local merge.alwaysours.driver true

Next, we need to tell git for which files to use this bogus merge driver. This is what the .gitattributes file is for. It supports the same small set of file patterns as the .gitignore file. For example

/configs/* merge=alwaysours
*.conf merge=alwaysours

would use our bogus merge driver for all files in '/config' or all files ending with '.conf'. It makes sense to also add this file to your git repository

git add .gitattributes

so that changes to it are tracked. There is one caveat though: when you merge in a branch having the .gitattributes file, the alwaysours driver will also be used on this branch! To avoid any problems arising from that, add the alwaysours merge driver for .gitattributes on all branches.

echo '/.gitattributes merge=alwaysours' > .gitattributes
git commit .gitattributes -m 'Register alwaysours driver for .gitattributes'

Of course this will break proper handling of any other settings you have in .gitattributes.

user1978011
  • 3,419
  • 25
  • 38
  • 1
    Hey just a question about this, after I do the commit. the files don't get overwritten which is good but is always asking me to push my changes which I don't want to do that because I don't want to push .gitattributes or the file that I don't want to get changed. How can I avoid this? – VaTo May 22 '15 at 22:49
  • @SaulOrtega then you should not have committed your gitattributes file. – user1978011 May 23 '15 at 12:48
  • 1
    Didn't you say that I have to commit it? git commit .gitattributes -m 'Register alwaysours driver for .gitattributes' – VaTo May 26 '15 at 04:45