Git subtree merge strategy, possible without mergi

2019-03-21 06:23发布

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 ?

2条回答
贪生不怕死
2楼-- · 2019-03-21 06:39

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"
查看更多
时光不老,我们不散
3楼-- · 2019-03-21 06:43

If it is just an annoyance when reading 'git log', then you might want to try:

git log --first-parent

It only shows your commits and the (single) merge commits, but not the commits of the remote.

If you want to see it with a diff (which will create one big diff for the merge commits):

git log -p -m --first-parent

No extra tools needed :)

查看更多
登录 后发表回答