简体   繁体   English

如何从现有git存储库中的文件夹创建新的git存储库?

[英]How do I create a new git repository from a folder in an existing git repository?

I want to create a new git repository of a folder under an existing git repository. 我想在现有git存储库下创建一个文件夹的新git存储库。 I want the history to be preserved in the new repository. 我希望将历史记录保存在新的存储库中。 How do I create this new git repository? 如何创建这个新的git存储库?

You can clone it, and then apply on the clone a filter-branch in order to split that repository , extracting only the directory you want (with its full history). 您可以克隆它,然后在克隆上应用筛选器分支以拆分该存储库 ,仅提取所需的目录(及其完整历史记录)。

See Detach subdirectory into separate Git repository 请参阅将子目录分离到单独的Git存储库中

Note the git clone --no-hardlinks /XYZ /ABC used during the cloning step: 注意克隆步骤中使用的git clone --no-hardlinks /XYZ /ABC

The --no-hardlinks switch makes git use real file copies instead of hardlinking when cloning a local repository. --no-hardlinks开关使git在克隆本地存储库时使用真实文件副本而不是硬链接。

Assuming you only want to keep the subdirectory as a separate project: you can use git filter-branch for that. 假设您只想将子目录保留为一个单独的项目:您可以使用git filter-branch

  1. Create a new branch 创建一个新分支
  2. Define a "tree filter" that rearranges a checked out copy the way you want, ideally as a separate script 定义一个“树过滤器”,以所需的方式重新排列已签出的副本,最好作为单独的脚本
  3. Run git filter-branch --tree-filter=/path/to/script thebranch 运行git filter-branch --tree-filter=/path/to/script thebranch

You now have a separate branch that is not related to the normal development stream, containing just the subdirectory. 现在,您有一个与常规开发流无关的单独分支,仅包含子目录。 Whoever clones the new branch gets only the subproject history. 克隆新分支的人将仅获得子项目历史记录。

You can clone it and keep the files you want; 您可以克隆它并保留所需的文件。 history will then be preserved. 历史将被保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM