Search code examples
git-subtree

git subtree: add different branches from a project at different prefixes


I have a module repository with some branches branchA, branchB etc. I would like to import these branches into a project repository using git subtree. I set up my project repository like this:

git remote add module /path/to/module.git
git fetch module
git subtree add -P folderA module/branchA
git subtree add -P folderB module/branchB

This seems to work as expected, and I've got files and history from the correct branches in folderA and folderB.

Now I have changes in the master branch of module, which I apply to both branchA and branchB (by rebasing branchA and branchB) and want to update project.

git fetch module

fetches these changes into project. However, when I then do

git subtree pull -P folderA module branchA
git subtree pull -P folderB module branchB

only folderA gets updated. For folderB, I get the following message:

From /path/to/module.git
 * branch            branchB -> FETCH_HEAD
Already up-to-date.

and the changes are not merged. Apparently, git is not correctly taking into account that the changes had been applied to path folderA, but not folderB. Does anyone have an idea what I could do to make this scenario work?


Solution

  • In reply to myself:

    It seems the solution was buried somewhere in the documentation of the original contribution to git-subtree at github repository for git-subtree (outdated). There, it reads

    OPTIONS FOR add, merge, push, pull

    --squash::

    This option is only valid for add, merge, push and pull commands.

    ...

    Using '--squash' also helps avoid problems when the same subproject is included multiple times in the same project, or is removed and then re-added. In such a case, it doesn't make sense to combine the histories anyway, since it's unclear which part of the history belongs to which subtree.

    Adding --squash to the git subtree commands pulls in the correct history into the right places.