我如何将一个目录从一个git存储库复制到另一个目录,保存历史记录? [英] How can I copy a directory from one git repository to another, preserving history?

查看:620
本文介绍了我如何将一个目录从一个git存储库复制到另一个目录,保存历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以从git仓库的目录中创建一个新的仓库。请参阅此处: https://help.github .com / articles / splitting-a-subfolder-out-a-new-repository /

然而,如何从一个目录复制目录存储库到另一个完全不同的存储库的新目录,同时保持该目录的历史记录?



更新:是否可以使用git log显示历史记录?

解决方案

你可以用 git filter-branch 来做到这一点。基本上你会想:


  1. 使用你已经找到的链接将第一个项目中的子路径拆分成一个新的存储库。
  2. 将该分支推送到第二个项目的远程分支上。

  3. 将该分支获取到第二个存储库中,然后使用 git filter-branch 将其过滤到正确的子目录中:

      git filter-branch --index-filter'
    git ls-files -sz |
    perl -0pes {\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. 最后,签出第二个项目的 master 分支(或者你正在使用的任何分支),然后合并到新过滤的分支中。


真的不是太可怕的操作,全都告诉了。正如AlexanderGladysh在评论中指出的那样,您还可以使用子树合并策略代替步骤3和4.

I know that I can create a new repository out of a directory of a git repository. See here: https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository/

However, how can I copy a directory from one repository to a new directory of another completely different repository, while keeping history of that directory?

Update: is it possible for that history to show up with git log?

解决方案

You could do this with git filter-branch. Basically you'd want to:

  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.

Really not too awful an operation, all told. As AlexanderGladysh notes in the comments, you could also use a subtree merging strategy in the place of steps 3 and 4.

这篇关于我如何将一个目录从一个git存储库复制到另一个目录,保存历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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