1

i want a specified file always raise conflict when it have any changes, i search in the internet and i find merge.*.driver in .gitattribute, but this configuration is only effect when this file both changed in two branch, what i wanted is when the file is only change in one branch(in other word,there is no conflict in the file), git also invoke the driver that configured in .gitattribute and raise conflict.

the application scenarios is i want monitor the changes of specified file, and remind me of conflict when merge.

my project git branch structure as follow:

prod:  init --- p1 --- p2  
         |  
uat:      ------u1 ----u2

when i receive a feature task, i create a new branch from prod branch, named "f1"
when i transfer my code to uat , i merge the branch f1 base on uat
when i transfer my code to prod , i merge the branch f1 base on prod
there are only one log configuration file (like log4j2.xml) in each branch,
so when i develop in branch f1, i will change the log config fit my local environment,
when i transfer to uat, my local config will merge to uat (what i do not want to happen )
"ours" merge strategy is worked, when i not add something in log config file,
but when i add something, "ours" strategy also discard what i added.
and "merge.*.driver" is only worked there have changes in both brach.

so i want to know there is any solution to deal with this situation.
like if any change is exist of specific file when merge is triggered, git always raise conflict

sdfqwe
  • 11
  • 2
  • 2
    Don't track deployment-specific configs. It's that simple. Conditional include of a local file is the usual way, or you could adopt [this method](https://stackoverflow.com/q/20078756/1290731) or [this one](https://stackoverflow.com/q/15150317/1290731). – jthill Jan 29 '22 at 17:10

1 Answers1

1

There is no way to do this. Git's merge algorithm considers three points: the two branches and a third commit, known as the merge base, which is usually the most recent common ancestor. Git can invoke a custom merge driver, but only if it thinks the file needs to be merged. It has a special case that if a file in one branch has the exact same object ID as the merge base and the other doesn't, then it takes the other side's branch because it knows only one side has changed. As a result, your merge driver will never get called in this case.

The way you typically deal with this case is to simply have three separate configuration files, all named things other than the actual filename you want. You then ignore the actual file you want. Then, you can use a script to copy the proper configuration files into the desired location based on the environment they're in (e.g., using an environment variable, a file on the machine, or the hostname).

Alternatively, you can create a single template file, again ignoring the actual file you're planning to use, and then use a script to turn that template file into the real file you want.

bk2204
  • 64,793
  • 6
  • 84
  • 100