7

We have a staging environment that auto-merges new branches into our staging branch and deploy our website on a staging server.

A new branch is based off our current master branch and then auto-merged into staging with git merge -X ours [new branch]. We got two files where we want to have a union merge. Meaning that, all changes from a branch must be copied wholly into a file.

So I have added those two files to .gitattributes:

allCampaigns.json merge=union
f-hero.css merge=union

I thought that git merge -X ours would apply to all files except allCampaigns.json and f-hero.css but that is not the case. In the case of allCampaigns.json, new JSON is overwriting another branch JSON, in the case of git merge -X theirs and omitted completely in the case of git merge -X ours.

Just for testing, I don't want a union merge for all files, I tried git merge -X union but that gives me a fatal error: fatal: Unknown option for merge-recursive: -Xunion.

This, git merge, keep both, SO question is exactly what I want but the answer is to add merge=union in .gitattributes which I have done but it is not working.

I'm using git version 2.30.2 and our server (which does the merging) is using git version 2.25.1 - both are linux machines.

It is almost as union merge does not exist.

How am I suppose to use the union merge driver?

File Overview

Below is a simplified overview of the website files. It shows files and folders at the root level, plus the location of f-hero.css.

.
├── .editorconfig
├── .git
├── .gitattributes
├── .gitignore
├── AWS_S3/
├── Makefile
├── README.md
├── allCampaigns.json
├── allCampaigns.schema.json
├── app.js
├── bin/
├── config/
├── node_modules/
├── package-lock.json
├── package.json
├── post-mortem.md
├── public/
├── server/
├── spec/
├── styles/
│   ├── base.css
│   └── blocks/
│       ├── f-hero.css
│       └── f-white-card.css
├── tools/
└── views/
dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54
  • I found `git merge-file` https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-merge-file.html but it appears that all of the files that are involved somehow has to be in the same file system, which different branches are not.. – dotnetCarpenter Mar 06 '22 at 11:17
  • "So I have added those two files to .gitattributes" There isn't usually a _.gitattributes_ file, so you would have had to _make_ one, I presume. But putting the _.gitattributes_ file in the right place is not trivial. Where did you put yours? – matt Mar 06 '22 at 16:33
  • I created it at the root of the project. Right next to the `.git` folder ;-) – dotnetCarpenter Mar 06 '22 at 17:09
  • Maybe this [answer](https://stackoverflow.com/a/13264117/4768943) might help a little. – Fırat Kocabay Mar 09 '22 at 20:24
  • Thank you trying to help. I actually link to that answer but I can’t get it to work. It works for the first branch but the next branch that gets merged will overwrite the first. I’m not even sure that git is doing a union merge. How can validate that it actually does when I loose content? – dotnetCarpenter Mar 10 '22 at 11:44
  • 2
    The `-X` option of `git-merge` specifies merge strategy options. `ours` is an option for the `recursive` merge strategy, while `union` is not - it's a built-in merge driver only meant to be specified with `.gitattributes`. For your problem, I tried [creating a minimal example](https://gist.github.com/jbrobst/63b67d005e1208464e37d7cb9c0df9af), but the merge conflicts resolved as expected. Could you provide an example of a merge conflict that was resolved incorrectly? – Josh Brobst Mar 11 '22 at 22:35

0 Answers0