我如何将一个目录从一个git存储库复制到另一个目录,保存历史记录? [英] How can I copy a directory from one git repository to another, preserving history?
问题描述
我知道我可以从git仓库的目录中创建一个新的仓库。请参阅此处: https://help.github .com / articles / splitting-a-subfolder-out-a-new-repository /
然而,如何从一个目录复制目录存储库到另一个完全不同的存储库的新目录,同时保持该目录的历史记录?
更新:是否可以使用git log显示历史记录?
你可以用 git filter-branch
来做到这一点。基本上你会想:
- 使用你已经找到的链接将第一个项目中的子路径拆分成一个新的存储库。
- 将该分支推送到第二个项目的远程分支上。
-
将该分支获取到第二个存储库中,然后使用
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
- 最后,签出第二个项目的
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:
- Split out the subpath in the first project into a new repository, using the link you've already found.
- Push it to the second project's remote on a unique branch.
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
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屋!