Search code examples
gitgit-merge

Git Ignore some part of the file


Is there a way to avoid some part of a file to be ignored while merging branches,

i.e. config.ini (just invented), suppose that I can't make config.dev.ini, config.test.ini and in all branches I only have config.ini file.

and in config.ini we have 'environment= dev/test/prod1..' one of it.

Every time merging from dev to test/master there is a conflict occur on config.ini and showing environment changed as dev or test or.. based on source branch.

Is there a way to ignore that part from conflict detection but keep other lines still in consideration. Is my question against SCM foundations? We're able to ignore files totally, why not partially?

I don't wanna put it in .gitignore.I just gave this config.ini thing as an example. However, there are some parts in some files I'm trying to avoid other developers overwrite it. Without ignoring the file totally. Of course for config.ini a programtic solution can be produced but I wondered existence of a solution for such cases in general.


Solution

  • No, there isn't a way to do this easily. You can use a custom merge driver and wire it up in .gitattributes, but that will mean you have to write a custom merge implementation and ask every user to install it on their system, which is a hassle, and it's also not going to be used by any hosting platform you use.

    This has been asked on the Git list, and the answer is essentially, “Don't do that.” What you're asking for is similar to the Git FAQ question on why you can't ignore changes to tracked files, but more generally, the answer is that you should do one of the following:

    • Use different files and copy one of them into an ignored location using a script, depending on the environment.
    • Generate the config file from a template on each system using a script.
    • Use configuration from the environment, which is a best practice anyway.