Search code examples
gitgitlabgit-mergemerge-strategy

GitLab - ignore specific files in target branch


I am using GitLab in my project and I have a quite specific workflow. I have 2 branches "Dev" and "Release" as shown below. When I submit a merge request from Dev to Release, the config files are also getting overwritten with that of Dev branch.

enter image description here

But ideally config files have to be different for both of these branches. I tried to use merge=ours. But it didn't serve my purpose as it will not override until and unless there is a conflict in config files. I don't want to add these files in .gitignore, as any changes to these files will not be committed in Dev branch. Can someone please guide me how to achieve this?


Solution

  • You can simply make sure your config files for dev and master are named differently, with their own history. That way, a merge will never overwrite anything.

    You can then version and track:

    • a config.tpl (template file with placeholder values).
    • two config file, one for dev, one for master
    • a script able to take a config file and a template file, and produce the config file (which remains untracked, private)
    • a .gitignore which ignores the resulting generated config file
    • a .gitattribute declaring a smudge content filter (see below)

    The generation of the config file is automated through a content filter driver, using a .gitattributes declaration.

    https://i.sstatic.net/tumAc.png
    (image from "Customizing Git - Git Attributes" from "Pro Git book"))

    Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your config file for you.
    See a complete example in "Best practice - Git + Build automation - Keeping configs separate".