Git子树合并策略,可以不合并历史记录吗?

发布于 2024-09-19 18:25:41 字数 352 浏览 4 评论 0原文

我一直在尝试摆脱子模块以获得独立的存储库,以及 子树合并策略似乎符合这个用例。

然而,合并的存储库的历史出现在我自己的项目的历史中,这相当烦人。

我尝试过 git filter-branch --subdirectory-filter path/to/subtree/ HEAD ,它有效......直到我尝试使用 git pull -s subtree my- 更新子树subtree master 它将所有子树的文件重写到我的项目的根目录。

有没有办法在 git 中本地实现这一点?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-09-26 18:25:41

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 子树 在它生成的提交的提交消息中记录额外信息;这些额外的信息使其能够有效地进行合并,而无需合并正在合并的子树的实际历史记录。


如果您知道您永远不会对“超级”存储库中的子树进行任何更改(即子树中的所有更改将始终来自其他存储库),那么您可以不用 git subtree并只需重复子树合并方法的 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"

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"
极致的悲 2024-09-26 18:25:41

如果这只是阅读“git log”时的烦恼,那么您可能想尝试:

git log --first-parent

它只显示您的提交和(单个)合并提交,但不显示远程的提交。

如果您想通过差异查看它(这将为合并提交创建一个大差异):

git log -p -m --first-parent

不需要额外的工具:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文