How to Auto-Resolve Git Subtree Conflicts with Three-Way Merge Strategy?

54 views Asked by At

Hello Stack Overflow community,

I'm currently dealing with a scenario involving Git subtrees and conflicts. Here's a breakdown of my situation: I have a repository A that includes repositories B and C as subtrees. I added repo B as a subtree to repo A using the command:

git subtree add --prefix repoB repoB develop --squash

So, the structure looks like this:

- A
  -- B

I made a commit in repo A within the subtree B, but I haven't pushed it to repo B yet. Additionally, I made some commits directly in repo B. In the worst-case scenario, these two sets of changes touch the same file. Finally, I attempted to pull the subtree from repo B into repo A using:

git subtree pull --prefix repoB repoB develop --squash

This resulted in a conflict:

CONFLICT (content): Merge conflict in repoA/repoB/someFile
Automatic merge failed; fix conflicts and then commit the result.
fatal: working tree has modifications. Cannot add.

Now, I want to automatically accept the changes from repo B when resolving conflicts, ideally using a three-way merge strategy. I've tried using --strategy-option theirs without success.

Is there a way to achieve automatic conflict resolution with a three-way merge strategy in Git subtree? If so, how can I implement it?

Any insights or alternative approaches would be greatly appreciated. Thank you!

1

There are 1 answers

0
philb On

The git subtree contrib command (i.e. not officially part of Git) indeed does not support --strategy-option (-X). It would not be too hard to support it though, it would just be a matter of passing the strategy to git merge here:

https://github.com/git/git/blob/624eb90fa8f65a79396615f3c2842ac5a3743350/contrib/subtree/git-subtree.sh#L1052-L1058

and wiring that up to the script options. Modifying the script is the only way I see to fully automate this.

If you do not want to work with a patched copy of git subtree, then you just need to resolve the conflict using "theirs" side when conflict arise. This can be done the same way as any other merge conflict, if you want something automatic then see this question: How to resolve git merge conflict, from command line, using a given strategy, for just one file?.