如何将一个带有历史记录的文件夹从一个Git仓库复制到另一个仓库?

8

GitHub网址现在是:https://help.github.com/articles/splitting-a-subpath-out-into-a-new-repository - Daniel Thorpe
1个回答

8
您可以使用git filter-branch来完成这个操作。基本上,您需要执行以下操作:
  1. Split out the subpath in the first project into a new repository, using the link you've already found.
  2. Push it to the second project's remote on a unique branch.
  3. Fetch that branch into the second repository, then use git filter-branch to index-filter it into the correct subdirectory:

    git filter-branch --index-filter '
        git ls-files -sz | 
        perl -0pe "s{\t}{\tnewsubdir/}" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --clear -z --index-info &&
            mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
    ' HEAD
    
  4. Finally, checkout the master branch of the second project (or whatever branch you're using), then merge in the newly-filtered branch.

总的来说,这并不是一项太糟糕的操作。正如AlexanderGladysh在评论中提到的那样,您也可以使用子树合并策略来替代步骤3和4。


@AlexanderGladysh 是的。您可以使用子树合并策略来完成。我已相应地编辑了答案。 - Christopher
嗯,我决定采用子树合并策略。一个问题是该目录上的git log仅显示最后一次合并提交而不是整个历史记录 :(. 这是否是预期的? - namin
使用filter-branch的解决方案似乎可以通过一些调整来实现:特别是在第3步之后,我现在已经将每个文件复制到“.”和“newsubdir”中。 - namin
@namin 是的。在 git log 中仅显示合并提交作为历史记录是子树合并策略的副产品。这实际上是我建议使用 filter-branch 而不是子树合并的原因。 - Christopher
@namin 修复了 update-index 命令的标志,现在该命令应该可以正常工作了。我之前是从一个需要特定 git 补丁的 git 列表线程中复制了该命令。对此我感到很抱歉。如果以上修改还不能正常工作,请在这里留言,我会直接尝试。基本上它应该与 filter-branch 手册完全相同,只不过使用 perl 而不是 sedsed\t 的解释取决于您的操作系统。 - Christopher
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接