Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

git: How do I add a custom merge strategy?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...