PaulJ
PaulJ

Reputation: 1717

git attributes for merge=ours doesn't work

I am using Sourcetree 3.0.12 on Windows. The command-line git version I'm using is git version 2.18.0.windows.1 (at least that's what it says when I open the Terminal from Sourcetree and get the Cygwin command line).

I want all the files on app/Resources/ in my tree to be "protected" from changes in other branches; in other words, I want git to always merge my version in that folder. So I did the following:

1) In the command line, at the root of the tree, I typed git config --global merge.ours.driver true.

2) At the root of my tree I created a .gitattributes file that said:

app/Resources/ merge=ours

Then I try to merge a change in another branch... and I get a lot of conflicts in files in app/Resources.

I read around an offhand comment suggesting that this feature only works with a newer version of git. Is that the case, or did I do something wrong?

EDIT: following the first answer below, I edited by hand my .git/config file to add this:

[merge "ours"]
    name = ours
    driver = true

I added it at the end of the file, then I tried merging again... but didn't work either.

Upvotes: 1

Views: 1011

Answers (2)

Erwan Leroux
Erwan Leroux

Reputation: 132

You should add

app/Resources/* merge=ours

in your .gitattributes.

Upvotes: 0

Daemon Painter
Daemon Painter

Reputation: 3520

Looks like you are trying to define a custom merge driver there. The docs tell that you should do this in another file, then reference it. This holds even in version 2.18.0.

The definition of a merge driver is done in the .git/config file, not in the gitattributes file, so strictly speaking this manual page is a wrong place to talk about it. ref

If I have a look at the examples, I see that:

ab* merge=filfre
abc -foo -bar
*.c frotz

Where that filfre was defined in the git config file. This should at least address the part 2 of your question, I'd advice you to try this different approach there.

For part 1, git config --global merge.ours.driver true was given as accepted answer here, although I feel like the other answer is way more accurate. Indeed, the accepted answer is by the original question author, and doesn't provide much inside. A known corner case is also available where the fix doesn't work. Have you considered the alternative approach?

Upvotes: 1

Related Questions