如何将单个目录从 git 存储库移动到新存储库,同时保留历史记录?

发布于 2024-07-17 18:56:09 字数 188 浏览 7 评论 0原文

我继承了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日裸衫吸 2024-07-24 18:56:09

您可以使用 git filter-branch 重写一个项目的历史。 从文档中:

重写存储库以使其看起来像
如果 foodir/ 是它的项目根,
并丢弃所有其他历史记录:

git filter-branch --subdirectory-filter foodir -- --all 
  

制作您的存储库的多个副本,对您想要拆分的每个子目录执行此操作,您应该最终得到您正在寻找的内容。

You can use git filter-branch to rewrite the history of a project. From the documentation:

To rewrite the repository to look as
if foodir/ had been its project root,
and discard all other history:

git filter-branch --subdirectory-filter foodir -- --all

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.

末骤雨初歇 2024-07-24 18:56:09

要将文件夹导出为新存储库,您需要:

  1. 克隆要导出的文件夹所在的存储库。
  2. 在托管提供商上创建一个空存储库(如 GitHub),以存储导出的文件夹。
  3. 打开克隆的存储库文件夹并运行以下命令:

    git 子树推送 --prefix=YourFolderNameToExport https://github.com/YourUserName/YourNewCleanRepoName master 
      

To export a folder as a new repository you need:

  1. To clone the repository where the folder you want to export is.
  2. To create a empty repository on your hosting provider as GitHub, to store the exported folder.
  3. Open the cloned repository folder and run this command:

    git subtree push --prefix=YourFolderNameToExport https://github.com/YourUserName/YourNewCleanRepoName master
    
墨小沫ゞ 2024-07-24 18:56:09

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文