如何将单个目录从 git 存储库移动到新存储库,同时保留历史记录?
我继承了一个 git 存储库,其中包含位于不同目录中的多个项目。 我想将存储库拆分为新的单独存储库,每个项目一个,然后让主存储库将项目作为子模块包含。 如果可能的话,我想在完成这一切的同时保留各个项目的修订历史记录。
我可以克隆每个项目的存储库并每次删除所有其他项目,但是是否有更好的方法来避免在每个新项目存储库中都有克隆的历史记录?
I have inherited a git repository containing multiple projects in separate directories. I'd like to split the repository into new individual repositories, one for each project and then have the master repository contain the projects as submodules. I'd like to do all this whilst maintaining the revision history of the individual projects if possible.
I could clone the repository for each project and remove all the other projects each time, but it there a better way to avoid having the cloned history in each new project repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
git filter-branch
重写一个项目的历史。 从文档中:制作您的存储库的多个副本,对您想要拆分的每个子目录执行此操作,您应该最终得到您正在寻找的内容。
You can use
git filter-branch
to rewrite the history of a project. From the documentation:Make several copies of your repo, do that for each subdirectory you want to split out, and you should wind up with what you're looking for.
要将文件夹导出为新存储库,您需要:
打开克隆的存储库文件夹并运行以下命令:
To export a folder as a new repository you need:
Open the cloned repository folder and run this command:
git 的要点在于,历史记录通过对父提交进行哈希处理而体现在每次提交中。 您可以将提交“重放”到新存储库中(这本质上是 svn-importer 的工作方式),并且只保留每个子项目。 然而,这会破坏提交哈希的含义。 如果你对此没有问题,那就这样吧。
过去我只是克隆它并继续前进。 这使得事情变得更大,但磁盘空间很便宜; 我的时间很贵。
我也不知道有什么工具可以拼接目录。 我想您可以在目录上 git-log 查找其上的所有提交,然后使用 git-fast-export 之类的东西重播提交?
The point of git is that the history is embodied in each commit by hashing the parent commit. You could "replay" the commits (this is essentially how the svn-importer works) into a new repository and only keeping each sub-project. This, however, would destroy the meaning of the commit hashes. If you have no problem with that then so be it.
In the past I've just cloned it and moved on. This makes things larger but disk space is cheap; my time is expensive.
I also don't know of any tools to splice out a directory. I suppose you could git-log on the directory to find all commits on it then replay the commits with something like git-fast-export?