GIT Split Repository目录保留*移动/重命名*历史记录 [英] GIT Split Repository directory preserving *move / renames* history

查看:68
本文介绍了GIT Split Repository目录保留*移动/重命名*历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您拥有存储库:

myCode/megaProject/moduleA
myCode/megaProject/moduleB

随着时间的推移(月),您将重新组织项目.重构代码以使模块独立. megaProject目录中的文件将移到它们自己的目录中.强调移动-这些文件的历史记录得以保留.

Over time (months), you re-organise the project. Refactoring the code to make the modules independent. Files in the megaProject directory get moved into their own directories. Emphasis on move - the history of these files is preserved.

myCode/megaProject
myCode/moduleA
myCode/moduleB

现在,您希望将这些模块移至其自己的GIT仓库.独自保留原来的megaProject.

Now you wish to move these modules to their own GIT repos. Leaving the original with just megaProject on its own.

myCode/megaProject
newRepoA/moduleA
newRepoB/moduleB

已记录了filter-branch命令可以执行此操作,但是在将文件移动到目标目录之外时,它不遵循历史记录.因此,历史记录是从文件移到新目录时开始的,而不是文件原来存放在旧megaProject目录中的历史记录.

The filter-branch command is documentated to do this but it doesn't follow history when files were moved outside of the target directory. So the history begins when the files were moved into their new directory, not the history the files had then they lived in the old megaProject directory.

如何根据目标目录拆分GIT历史记录,并遵循此路径之外的历史记录-仅保留与这些文件相关的提交历史记录?

How to split a GIT history based on a target directory, and, follow history outside of this path - leaving only commit history related to these files and nothing else?

关于SO的许多其他答案都集中在通常将回购协议拆分-但没有提及拆分并遵循移动历史.

The numerous other answers on SO focus on generally splitting apart the repo - but make no mention of splitting apart and following the move history.

推荐答案

在克隆的存储库中运行git filter-branch --subdirectory-filter会删除所有不影响该子目录中内容的提交,包括那些在文件移动之前影响文件的提交.

Running git filter-branch --subdirectory-filter in your cloned repository will remove all commits that don't affect content in that subdirectory, which includes those affecting the files before they were moved.

相反,您需要在脚本中使用--index-filter标志来删除所有您不感兴趣的文件,并使用--prune-empty标志来忽略任何影响其他内容的提交.

Instead, you need to use the --index-filter flag with a script to delete all files you're not interested in, and the --prune-empty flag to ignore any commits affecting other content.

Kevin Deldycke的博客帖子很好的例子:

There's a blog post from Kevin Deldycke with a good example of this:

git filter-branch --prune-empty --tree-filter 'find ./ -maxdepth 1 -not -path "./e107*" -and -not -path "./wordpress-e107*" -and -not -path "./.git" -and -not -path "./" -print -exec rm -rf "{}" \;' -- --all

此命令依次有效地检出每个提交,从工作目录中删除所有无用的文件,并且,如果上一次提交有任何更改,则它将检入(沿历史记录进行重写).您需要调整该命令以删除除/moduleA/megaProject/moduleA中的文件以及要从/megaProject中保留的特定文件之外的所有文件.

This command effectively checks out each commit in turn, deletes all uninteresting files from the working directory and, if anything has changed from the last commit then it checks it in (rewriting the history as it goes). You would need to tweak that command to delete all files except those in, say, /moduleA, /megaProject/moduleA and the specific files you want to keep from /megaProject.

这篇关于GIT Split Repository目录保留*移动/重命名*历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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