views:

39

answers:

1

I've been trying to move away from submodules in order to get a self-contained repository, and the subtree merge strategy seems to match this use-case.

However the merged repos' histories appear in my own project's history, which is reather annoying.

I've tried git filter-branch --subdirectory-filter path/to/subtree/ HEAD which works... until I try to update a subtree with git pull -s subtree my-subtree master which rewrites all the subtree's files to my project's root.

Is there a way to achieve this natively in git ?

+1  A: 

apenwarr’s git subtree has a --squash option that might do very nearly what you want.

remote=libfoo
branch=master
prefix=helpers/libfoo

git fetch "$remote" &&
git subtree add --prefix="$prefix" --squash "$remote/$branch"

: Later, once there are changes to pick up.
git subtree pull --prefix="$prefix" --squash "$remote" "$branch"

git subtree records extra information in the commit messages of the commits it generates; this extra information allows it to effectively do merges without having to incorporate the actual history of the subtree being merged.


If you know that you will never make any changes to the subtree in the “super” repository (i.e. all the changes in the subtree will always come from some other repository), then you could do without git subtree and just repeat the git read-tree --prefix= part of the subtree merge method (though you have to clean out your current subtree from both the index first).

remote=libfoo
branch=master
prefix=helpers/libfoo/

: replace the local subtree with whatever the other repository has
git fetch "$remote" &&
git rm -r --ignore-unmatch "$prefix" &&
git read-tree --prefix="$prefix" "${remote}/${branch}" &&
git checkout -- "$prefix" &&
git commit -m "update $prefix from $remote $branch"
Chris Johnsen
I wish it were possible to maintain local branches for subtrees without relying on a dependency, seems like it's not possible to have your cake and eat it too here... I'll leave the question open for a couple of days though, in case someone comes up with some magical way to achieve this using git only. Thanks Chris.
julien