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.

  • git svn clone fails with “Failed to strip path”
  • How to determine last merged branch in git?
  • Migrate from old LFS repo to new LFS repo?
  • git locally, push to tfs remote repo
  • Is there a Github clone in PHP that I can run on my own server?
  • How does Git store tree objects?
  • 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

    thanks!

  • How to set the default branch in GitHub.com?
  • Merging changesets in git for a code review
  • How does Git(Hub) handle possible collisions from short SHAs?
  • git pre-commit hook format code - Intellij/Android Studio
  • How to push a single file in a subdirectory to Github (not master)
  • How to use specified key when working with github via portablegit?
  • 2 Solutions collect form web for “tell git to use ours merge strategy on specific files”

    $ git config merge.ours.driver true
    

    or even

    $ 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.

    Git Baby is a git and github fan, let's start git clone.