Whats the Proper usage of .gitattributes with merg

2019-01-24 06:17发布

I just tried using .gitattributes for the first time. I figure I must be doing something wrong because it is not working as I thought the purpose of using the merge=ours attribute was intended for (?).

An example of my usage:

I have two branches; one is the "master" and the other is "GCE-Site". There are files on the "GCE-Site" branch that have different settings that also exists on the "master" branch that I do not want merging into the "master" [or other branches].

On both branches, I have a .gitattributes containing the following rules:

README.md merge=ours
config.php merge=ours
.gitattributes merge=ours
.gitignore merge=ours
.cache/ merge=ours

All the files needing the rules are on the root level plus everything above the .cache/ folder. When I do a git merge GCE-Site, all files still merge into the master when I don't want that.

Is there something I am missing? Your help is greatly appreciated. Thnx

2条回答
可以哭但决不认输i
2楼-- · 2019-01-24 06:40

Easy fix once I knew what I was doing: I missed adding the merge strategy with git config --global merge.ours.driver true in my local environment.

查看更多
地球回转人心会变
3楼-- · 2019-01-24 06:51

As mentioned in ".gitattributes & individual merge strategy for a file":

The merge driver is only called in non-trivial cases, i.e. if both master and test have touched setup (and you need to define the merge driver ours first).

You can see an example of concurrent modification (ie modification in both branches) in "How do I tell git to always select my local version for conflicted merges on a specific file?".

So if your merges didn't involve modifications of the same files within cache/ folder in both branches, your merge driver was never called.

But in your case, for files with different settings, I would recommend not using a merge driver, but a content filter driver, as in "Keep settings in branch"

content filter driver

That allows you to keep in version control:

  • template file for the settings in the cache/ folder
  • a script able to take a template file and replace the value placeholders with the right value depending on the branch checked out.
查看更多
登录 后发表回答