Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: How do I add a custom merge strategy?

Tags:

git

merge

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?

like image 249
kgutwin Avatar asked Apr 17 '14 18:04

kgutwin


People also ask

What is the default git merge strategy?

Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies. This is the default merge strategy when pulling or merging one branch.

What is ORT strategy in git?

Git's new merge-ort strategy is a scratch rewrite of its recursive strategy but addresses correctness and performance problems. GitHub reports merge-ort can be as much as a "500x" speed-up for large merges with many renames. Merge-ort for merges in a re-base operation can be a speed-up of over 9000x.

What is alternative option for merging in git?

Using git rebase Instead of git merge. Using the "git merge" command is probably the easiest way to integrate changes from one branch into another. However, it's not your only option: "git rebase" offers another, slightly different way of integration.


1 Answers

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
like image 173
Edward Thomson Avatar answered Oct 25 '22 23:10

Edward Thomson