21

I'm trying to add a custom merge strategy similar to the one in this question: Git merge conflict to always take the newest file

I've saved the script as git-merge-latest.sh and added the following lines to .git/config:

[merge "latest"]
    name = select latest file merge driver
    driver = git-merge-latest.sh %O %A %B

However, when I run git pull --strategy latest, I get the message:

Could not find merge strategy 'latest'.
Available strategies are: octopus ours recursive resolve subtree.

I've tried checking git config merge.latest.driver, which returns the expected output. I also changed the value of driver to true just to verify that it wasn't a problem with finding the script.

This happens on two different systems running git 1.8.2.2 and 1.7.9.5. What am I doing wrong?

Community
  • 1
  • 1
kgutwin
  • 867
  • 1
  • 10
  • 15

1 Answers1

34

In this case, you didn't configure a merge strategy, you configured a merge driver:

A merge strategy is a program that determines how two (or more) commits are merged. By default, git merge uses the "recursive" strategy, found in the program git-merge-recursive. By specifying the --strategy <strategy> flag to git-merge (or git-pull) you tell it to invoke a different strategy. If you want to plug in your own merge strategy, you can, by creating an executable git-merge-mystrategy in your path and running git merge --strategy mystrategy.

This is different than a merge driver. A merge driver is the mechanism used to resolve a conflict on a file that exists when merging two commits. You plug in your own merge driver in the manner you outlined, by configuring a merge.mydriver.driver setting.

To enable your merge driver for a particular file, you need to configure the driver for that file in .gitattributes:

filename merge=mydriver
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Might want to explicitly state that adding a merge strategy is a matter of modifying the `git` code - it isn't something that can be done through configuration changes (at least currently)... – twalberg Apr 17 '14 at 20:09
  • 2
    @twalberg - you can plug your own quite easily, actually; if you have a `git-merge-xyz` in your path then git will run it if you `git merge --strategy xyz`. I will make that more clear in my answer. – Edward Thomson Apr 17 '14 at 21:58
  • 14
    Is the specification for how to write a git merge strategy executable documented anywhere? What should I expect on stdin, and what should be the output? – Asad Saeeduddin May 01 '17 at 18:50
  • Any way to set a default strategy for certain files? – DylanYoung Jan 24 '22 at 23:31
  • @AsadSaeeduddin From the [link](https://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver) in the answer: “The merge driver is expected to leave the result of the merge in the file named with %A by overwriting it, and exit with zero status if it managed to merge them cleanly, or non-zero if there were conflicts.” You can specify which arguments are passed to your executable through percent variable substitution. – Anton Strogonoff Jun 14 '23 at 09:44