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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/811251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:24:40  来源:igfitidea点击:

How can I move a single directory from a git repository to a new repository whilst maintaining the history?

gitgit-submodules

提问by cidered

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.

我继承了一个 git 存储库,其中包含不同目录中的多个项目。我想将存储库拆分为新的单独存储库,每个项目一个,然后让主存储库包含项目作为子模块。如果可能的话,我想在保持单个项目的修订历史的同时完成所有这些。

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?

我可以为每个项目克隆存储库并每次删除所有其他项目,但是有更好的方法来避免在每个新项目存储库中克隆历史记录吗?

采纳答案by Brian Campbell

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

您可以使用git filter-branch来重写项目的历史记录。从文档:

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

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

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.

为您的 repo 制作多个副本,为您要拆分的每个子目录执行此操作,然后您应该会找到您要查找的内容。

回答by user

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
    
  1. 克隆要导出的文件夹所在的存储库。
  2. 在您的托管服务提供商上创建一个空存储库作为 GitHub,以存储导出的文件夹。
  3. 打开克隆的存储库文件夹并运行以下命令:

    git subtree push --prefix=YourFolderNameToExport https://github.com/YourUserName/YourNewCleanRepoName master
    

回答by Colin Burnett

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.

git 的要点在于,通过对父提交进行哈希处理,历史记录体现在每个提交中。您可以将提交(这基本上是 svn-importer 的工作方式)“重放”到一个新的存储库中,并且只保留每个子项目。但是,这会破坏提交哈希的含义。如果你没有问题,那就这样吧。

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?

我也不知道有什么工具可以拼接目录。我想你可以 git-log 在目录上找到所有提交,然后用类似 git-fast-export 的东西重播提交?