Home > OS >  How to use merge=ours strategy in GitHub
How to use merge=ours strategy in GitHub

Time:12-08

Git provides an option using .gitattributes to ignore merge conflicts for certain files (ex: generated files) using merge strategies:

http://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Merge-Strategies

You can also use Git attributes to tell Git to use different merge strategies for specific files in your project. One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s.

You can set up an attribute like this:

database.xml merge=ours

And then define a dummy ours merge strategy with:

$ git config --global merge.ours.driver true

As you can see, this requires special configuration using git config on my local machine.

I'm trying to configure this to work on GitHub (i.e., where I don't have access to git config --global).

Is there a way I can enable a merge strategy for a pull request on GitHub?

Edit

According to How to store a git config as part of the repository? there is no default location for the Git config besides .git/config. So afaik local config is not possible to commit to a git repo.

CodePudding user response:

That's technically a merge driver, not a merge strategy; the wording in the Git Book is not great here.

The short answer is just "no" though. There is no way to do this directly on GitHub. Do the merge locally, then push the merge, instead of using a pull request.

Be aware that merge drivers are not used in some cases where you might expect them to be used. In particular, consider the following setup:

          I--J   <-- br1 (HEAD)
         /
...--G--H
         \
          K--L   <-- br2

You run git merge br2 to merge commit L into commit J, using commit H as the merge base. Assume that there is a .gitattributes that says that file F should be merged with an ours driver, and that you have defined a suitable ours driver.1

Now, assume that file F in commit J matches file F in commit H. But file F in commit L is different. For concreteness, let's say this file is named server.conf, and that it says hostip 1.2.3.4 in commit H and commit J, but hostip 8.7.6.5 in commit L.

Since you have server.conf driver=ours in your .gitattributes file, you might expect Git to preserve the hostip 1.2.3.4 from commit H and commit L in the final merge commit M. But it won't: Git will instead take the copy of F (server.conf) from commit L.

(I generally recommend avoiding merge drivers: they don't work as well as you might expect. If you find yourself needing one, you probably really need to rework your file setup.)


1We can ignore any technical worries about which .gitattributes is used by assuming that this .gitattributes is in place at commit H and unchanged throughout. (In practice, I believe Git uses the working tree .gitattributes if possible. Otherwise it uses the one from the current commit, in this case J.)

  • Related