Git子树合并策略,可能没有合并历史? [英] Git subtree merge strategy, possible without merging history?

查看:266
本文介绍了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.

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

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中实现这一目标?

Is there a way to achieve this natively in git ?

推荐答案

awarwarr git subtree 有一个 - squash 选项,它可能做得非常接近你想要的。

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 在其生成的提交的提交消息中记录额外的信息;这些额外的信息可以让它有效地进行合并,而不必合并被合并的子树的实际历史记录。

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.

如果你知道你永远不会对超级存储库中的子树进行任何更改(即,子树中的所有更改将始终来自其他存储库),那么您可以不使用 git子树重复子树合并方法的 git read-tree --prefix = 部分(尽管您必须先从两个索引中清除当前子树)。

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"

这篇关于Git子树合并策略,可能没有合并历史?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆