admin管理员组

文章数量:1313599

I have used Git for a long time but I am new to submodule/subtree and have to use it now. So I created a new repo to be used as a child repo. Then did the following:

  1. Created a subtree within a new (child) repo from existing main repo git subtree add -P MainRepo Development
  2. Posted a commit on a child side and dropped some files from within a child repo. Now when pulling main repo changes git subtree pull -P MainRepo Development I am getting conflicts since some files are deleted on a child repo side. This is an error example:
CONFLICT (modify/delete): CancellationRepository.cs deleted in HEAD and modified in
73b8646642887c850f5def4984778c3f36de90f. Version 673b8646642887c850f5def4984778c 
CancellationRepository.cs left in tree.
Automatic merge failed; fix conflicts and then commit the result.

Is there a way to setup subtree that conflicts are always silently resolved to a child side/deleted version?

Git repos are hosted within Azure Devops.

I have used Git for a long time but I am new to submodule/subtree and have to use it now. So I created a new repo to be used as a child repo. Then did the following:

  1. Created a subtree within a new (child) repo from existing main repo git subtree add -P MainRepo Development
  2. Posted a commit on a child side and dropped some files from within a child repo. Now when pulling main repo changes git subtree pull -P MainRepo Development I am getting conflicts since some files are deleted on a child repo side. This is an error example:
CONFLICT (modify/delete): CancellationRepository.cs deleted in HEAD and modified in
73b8646642887c850f5def4984778c3f36de90f. Version 673b8646642887c850f5def4984778c 
CancellationRepository.cs left in tree.
Automatic merge failed; fix conflicts and then commit the result.

Is there a way to setup subtree that conflicts are always silently resolved to a child side/deleted version?

Git repos are hosted within Azure Devops.

Share Improve this question edited Feb 4 at 2:58 Victor asked Jan 30 at 19:02 VictorVictor 9533 gold badges29 silver badges54 bronze badges 9
  • Is there any specific reason to use subtree over submodules? What do you mean by "silently resolved" - same as -X: stackoverflow/questions/10697463/… ? – Mo_ Commented Feb 2 at 20:38
  • Thanks for reply and a link. New to subtree/submodule both as stated, but subtree seems easier to coordinate changes on both sides. That question uses git pull but git subtree pull doesn't have that option. – Victor Commented Feb 2 at 22:45
  • 1 "git subtree pull doesn't have that option", hence I'm wondering why you're not trying to use a submodule instead of a subtree? With submodules you can use all the normal git commands you already know how to use. What are you trying to achieve by using sub[trees|modules] (What's your Y problem)? Most people use submodules to include a library or module into their main repo. Are you trying to do something similar to that? – Mo_ Commented Feb 3 at 6:01
  • 1 @Mo_ We have an existing repo that is our main codebase and now setting up a new secondary repo that will be synched up with main repo when there are new dev commits to be used for DevOps purposes. Subrepo should have a subset of files from main repo. From what I've seen so far (could be wrong) it seems that subtree is easier to keep synch up then submodule in this kind of scenario. Is that accurate? As far as MWE I've described above all the steps taken and command used, nothing else involved. – Victor Commented Feb 3 at 17:50
  • 1 @Mo_ We do both - development and DevOps. Looking at your test script that's exactly what I did. The only requirement - set execution policy to silently resolve conflicts on a child side otherwise we can't automate DevOps pipelines. Thanks for tip - I'll check subrepo out as well. – Victor Commented Feb 7 at 22:21
 |  Show 4 more comments

3 Answers 3

Reset to default 1

The answer to the question "setup subtree that conflicts are always silently resolved to a child side" is: The git subtree script in contrib seems not to support this as of 2025:

cmd_pull () {
    if test $# -ne 2
    then
        die "fatal: you must provide <repository> <ref>"
    fi

Even if you manage to do it some other way, you state your goal is to automate DevOps pipelines. At some point this will break, because -X theirs will get something wrong at some point. E.g. a lib dependency, a change to a file that depends on the deleted files, or some moved file etc.

However, to automate your DevOps pipeline, while removing the undesired files, there are ways to achieve it, which are probably simpler than bending subtree to suit your needs. If I got your requirements correctly, I'd do something like:

  • For DevOps clone mainRepo (or shallow clone if repo size is big)
  • Run a script/pipeline/something automated to remove the undesired files
  • Run your DevOps automation
  • If you need to do edits in the DevOps/ChildRepo do them on a separate branch that you merge manually, using your preferred merge strategy

If this does not work, I'd suggest you write the actual Y-Problem in a separate question or forum.

Try this:

git pull -X theirs
  • This is a straightforward and effective solution. Did you try this? If so, what error did you encounter?

You can perform the merge manually instead of relying on git subtree pull so that you can pass custom merge options.

For example, if you want Git to favor your current branch (the child side) when conflicts occur, you can do something like:

# Fetch the latest changes from the main working repository
git fetch <main-repo-remote> Development

# Merge the fetched commit using the subtree merge strategy and telling Git to prefer "ours" in case of conflict
git merge -s subtree -X ours FETCH_HEAD

Here, the -X ours option tells Git that if there is a conflict, it should take the version from the current branch (your child repo). This should automatically resolve the kind of conflict where a file has been deleted on your side.

Note: The -X ours option works on conflicting hunks and should resolve your “modify/delete” conflict in favor of the deletion. However, make sure you test it carefully to be sure that it works for your entire subtree.

本文标签: Git subtree conflict resolutionStack Overflow