代码之家  ›  专栏  ›  技术社区  ›  Doe Wayne

git将两个存储库合并为一个新的存储库,其中一个应该是一个分支

  •  2
  • Doe Wayne  · 技术社区  · 7 年前

    我想把存储库放在一起,但第二个存储库应该放在一个分支中

    A - B - C - D -  E -  F - G - H Repo1
    
               A1 - B1 - C1 Repo2
    

    克里特岛新回购

    git init reponew
    

    git remote add -f Repo1 ..\..\output\Repo1
    

    将Repo1/master中的文件合并到new/master中

    git merge Repo1/master --allow-unrelated-histories
    

    新的存储库reponew如下所示:

    A - B - C - D -  E -  F - G - H master
    

    现在,我在特定的提交和签出上创建了一个新分支

    git branch branchX 200303b0b215cc0fb2d92f50ffc9d7df8bbaab74
    git checkout branchX
    

    为旧回购1添加远程并获取

    git remote add -f Repo2 ..\..\output\Repo2
    

    git merge -Xtheirs Repo2/master --allow-unrelated-histories
    

    现在,新的存储库repnew如下所示:

                     A1 - B1 - C1 branchX
                                         \
    A - B - C  - - - - - - - - - - - - - -
              \
               D - E - - - - - F - G - H master
    

    看起来这是由合并日期为现在的合并请求引起的

    A - B - C - D - E - - - - - F - G - H master
              \
                - - - A1 - B1 - C1 branchX
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   zigarn    7 年前

    之后 要使用的现有历史记录 rebase
    但是 重新基准 onto . 因此,如果旧提交上仍有引用(分支或标记),则它们将在整个提交历史中保持可用。

    Repo2/master C .

    因此,解决方案是:

    git branch branchX --no-track Repo2/master
    git rebase -Xtheirs 200303b0b215cc0fb2d92f50ffc9d7df8bbaab74 branchX
    

    你应该得到你想要的结果。


    filter-branch . 滤波器支路 它也会移动标签。

    具有 滤波器支路 ,有一个 --parent-filter
    branchX : git tag --merged branchX --tag-name-filter cat .

    因此,命令将是:

    git branch branchX --no-track Repo2/master
    git filter-branch \
        --parent-filter 'test $GIT_COMMIT = <FULL-commit-id-of-A1> && echo "-p 200303b0b215cc0fb2d92f50ffc9d7df8bbaab74" || cat' \
        --tag-name-filter cat \
        -- branchX $(git tag --merged branchX)
    

    然后,您将拥有重写的历史,但仍然是旧的历史,带有前缀为的引用 refs/original/ .

    rm -rf .git/refs/original/ ),否则您可以还原它们( cp -r .git/refs/original/refs .git && rm -rf .git/refs/original/