tell git to use ours merge strategy on specific files
I have the following directory structure:
/.git /.git/info/attributes /MyProject /MyProject/pom.xml /MyProject/MyCode.java
I have branch master and bugfix. On both branches pom.xml and MyCode.java were modified.
i would like to merge changes from bugfix to master only for MyCode.java and keep master version of pom.xml file.
So I added “/.git/info/attributes” because i don’t want to commit .gitattributes with the project
$cat .git/info/attributes pom.xml merge=ours $git status # On branch master nothing to commit (working directory clean) $git check-attr -a pom.xml pom.xml: merge: ours
Finally to the problem:
When I do:
$git merge bugfix Auto-merging MyProject/pom.xml CONFLICT (content): Merge conflict in MyProject/pom.xml Automatic merge failed; fix conflicts and then commit the result.
Git is ignoring attributes setting. What am i missing here?
I prefer not to define “keepMine.sh” per this post
this post is what i need, however i prefer not to go file by file if i have a simple pattern
2 Solutions collect form web for “tell git to use ours merge strategy on specific files”
$ git config merge.ours.driver true
$ git config --global merge.ours.driver true
‘ours’ isn’t one of the built-in merge drivers even though it’s perfectly clear to you and me what it should do, and it seems git doesn’t error out when a custom merge driver is undefined.
true above is just the unix
true command, its success says it made the local version look right, in this case by doing nothing to it.)
From my testing with git version 1.7.1 I find that unless there IS a requirement to merge, no merge driver will execute (regardless of what you put in the config, and regardless of any git attributes).
If you branch out from the “master” to your own local branch “bugfix” and then edit a bunch of stuff in your own branch, commit that. Afterwards you checkout the “master” branch again, then do something like:
git merge bugfix
You might expect the “merge=ours” attribute to protect you in this situation. It won’t! Branch “bugfix” will stomp over branch “master” if there have been no edits to that particular file within the master branch since the time “bugfix” branched away from master. No edits implies no need to merge which implies don’t run a merge driver. This seems such a common problem that git should have a particular attribute pre-defined to give a file absolute local protection from foreign merges, under ALL situations.
FWIW, I’ve read other people’s opinions that config files should be local and private and not checked into git at all. I think that’s kind of bollox: of course I do want to track changes in my config file. Of course the idea of a collaborative tool is that I want to be able to compare my config against what other people in my team are running, or compare the development config to the production config. The whole idea of a tool is that it helps you get your work done.
As a kludgy solution, the best I’ve come up with is a config directory with each config file having a different name (i.e. config/production config/test config/devel_joe config/devel_bill and so on) and all of these go into git. Then at deployment the actual working config file is copied out of that config directory. I think it’s an ugly approach because it is a step backwards away from the idea of named branches, but at least it is safe.