简体   繁体   English

Git子树合并策略,可能没有合并历史?

[英]Git subtree merge strategy, possible without merging history?

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. 我已经尝试了git filter-branch --subdirectory-filter path/to/subtree/ HEAD ,它有效...直到我尝试使用git pull -s subtree my-subtree master更新子树,它将所有子树的文件重写为my项目的根。

Is there a way to achieve this natively in git ? 有没有办法在git中实现这一点?

apenwarr 's git subtree has a --squash option that might do very nearly what you want. apenwarrgit子树有一个--squash选项,可能几乎可以做你想要的。

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; git子树在它生成的提交的提交消息中记录额外信息; 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 (ie 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). 如果您知道永远不会对“超级”存储库中的子树进行任何更改(即子树中的所有更改将始终来自其他存储库),那么您可以在没有git子树的情况下进行更改并重复git read-tree --prefix=子树合并方法的一部分(尽管您必须先从索引中清除当前子树)。

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"

If it is just an annoyance when reading 'git log', then you might want to try: 如果在阅读'git log'时只是一个烦恼,那么你可能想尝试:

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): 如果你想用diff看到它(这将为合并提交创建一个大的差异):

git log -p -m --first-parent

No extra tools needed :) 无需额外的工具:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM