20

We previously used many submodules in our main repositories, but to increase the maintainability of our projects we started an experimental branch where we replaced them all with subtrees.

This worked good - but now when I'm trying to update one of the subtrees it erroneously merges the update into a completely wrong directory that isn't even a subtree.

The main repository, where the branch "subtree" contains the experimental branch, is: git://github.com/hugowetterberg/goodold_drupal.git

The repository to merge in updates from: git://github.com/voxpelli/drupal-oembed.git

Merging by doing: git merge -s subtree oembed/master

The path the updates should be merged into: sites/all/modules/oembed/

The path they are merged into: modules/aggregator/translations/

Anyone having an idea of how to get the updates into the subtrees or what the error can be?

VoxPelli
  • 2,697
  • 1
  • 16
  • 22

2 Answers2

20

Unfortunately this is a bug (or missing feature) in the "git merge -s subtree" code. It actually guesses the subtrees that you want to merge. Usually, this magically turns out to be correct, but if your subtree contains a lot of changes (or was originally empty, or whatever), then it can fail spectacularly.

The best way to work around it is:

  1. Merge the files as you did above.

  2. Manually move all the resulting files to where they should have gone.

  3. git commit -a --amend to correct the merge commit.

Future merges will probably work fine, unless this directory is constantly in unbelievable amounts of flux.

The experimental "git subtree" command has a --prefix parameter that should let you override this, but unfortunately it doesn't work at the moment (since it requires working around "git merge -s subtree" features and there hasn't been time to do it).

Anyway, this should be a rare situation and the workaround won't be needed even for future merges of the same project.

apenwarr
  • 10,838
  • 6
  • 47
  • 58
  • 7
    I just submitted a patch to git that will allow you to override the subtree explicitly using that '-Xsubtree=' option, and it was accepted. Look for it in git 1.7.0 or so. – apenwarr Dec 03 '09 at 22:53
  • 2
    When you bring in a single folder from the source repo (eg `git read-tree --prefix=dest/ -u source_repo/master:folder_in_repo/`) rather than the whole thing, merging even minor changes back to the repo seems to result in the whole folder being placed into a new location in the repo (ie duplicated). – Hari Honor Jan 09 '12 at 10:45
  • Non that rare situation, in fact :-( – Alexander Gladysh Apr 11 '12 at 11:05
  • 2
    Most painful merge ever when git gets this wrong. Trying `-X subtree=XX` helped me, thanks – shuckc Jul 09 '13 at 14:47
  • Thank you all guys. This was driving me nuts because with the luck I've got with my first subtree experiment I did naturally run into this buggy bug. – hakre Aug 19 '13 at 11:58
12

git version 1.7.9.5

git pull -s subtree <remote name> <remote branch>

(merge went into the wrong directory)

git reset --hard HEAD^
git pull -s subtree -Xsubtree=correct/directory <remote name> <remote branch>

Note that there is no trailing slash on the directory

Thanks apenwarr

http://git.661346.n2.nabble.com/PATCH-0-8-The-return-of-Xours-Xtheirs-Xsubtree-dir-td4069081.html

Community
  • 1
  • 1
FlipMcF
  • 12,636
  • 2
  • 35
  • 44