How do I prevent tracked config files from being changed by merging in git?

I have a web project with two git branches (development 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, exported SQL files, etc., which are different on both branches.

Every time I try to merge these branches, I get a lot of conflicts in these settings files that need to be manually resolved, but from time to time I miss something. In short, it's a nightmare. So my question is, how can I avoid this?

  • Is there a way to prevent any changes to these config files on a merge or reboot? *

Recommended solution: Msgstr "Try to make these files unplayable." I tried this, but whenever I pull from the production file, I get these files deleted and replaced, so I don't know if I should be doing it differently.

+3


source to share


1 answer


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

First you register the merge driver which

  • does nothing and
  • completes successfully.

The shell command true

is exactly what we are looking for. The following line registers true

as a merge driver alwaysours

:

git config --local merge.alwaysours.driver true

      

Next, we need to tell git which files to use this dummy merge driver for. This is the file .gitattributes

. It supports the same small set of file templates as a file .gitignore

. for example



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

      

will use our dummy merge driver for all files in '/ config' or all files ending in .conf. It makes sense to also add this file to the git repository

git add .gitattributes

      

so that changes in it are tracked. However, there is one caveat: when you merge into a branch with a file .gitattributes

, that branch will also use the driver alwaysours

! To avoid any problems associated with it, add a merge driver alwaysours

for .gitattributes

all branches.

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

      

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

.

+1


source







All Articles