【问题标题】:Git subtree merge strategy, possible without merging history?Git子树合并策略,可以不合并历史?
【发布时间】:2023-03-28 03:20:01
【问题描述】:

我一直在尝试远离子模块以获得一个独立的存储库,subtree merge strategy 似乎与这个用例相匹配。

但是,合并后的 repos 的历史记录出现在我自己项目的历史记录中,这很烦人。

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

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

【问题讨论】:

    标签: git history git-submodules subtree


    【解决方案1】:

    apenwarrgit subtree 有一个 --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 在它生成的提交的提交消息中记录额外的信息;这些额外的信息允许它有效地进行合并,而不必包含被合并子树的实际历史记录。


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

    【讨论】:

    • 我希望可以在不依赖依赖的情况下维护子树的本地分支,似乎不可能在这里吃到你的蛋糕......我会留下这个问题不过几天后,以防有人想出一些神奇的方法来仅使用 git 来实现这一目标。谢谢克里斯。
    【解决方案2】:

    如果只是在阅读'git log'时感到烦恼,那么您可能想尝试一下:

    git log --first-parent
    

    它只显示你的提交和(单个)合并提交,而不是远程的提交。

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

    git log -p -m --first-parent
    

    不需要额外的工具:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2017-01-09
      • 2020-02-05
      相关资源
      最近更新 更多