如何使现有 Git 存储库的一部分成为子模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1593620/
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-19 03:54:19  来源:igfitidea点击:

How to make part of an existing Git repository a submodule

gitgit-submodules

提问by soulnafein

I have this Git repository which contains two folders: binary-searchand poker.

我有这个 Git 存储库,其中包含两个文件夹:binary-searchpoker.

For example: https://github.com/soulnafein/code-katas

例如:https: //github.com/soulnafein/code-katas

I would like to turn these folders into submodules and keep their change history.

我想将这些文件夹变成子模块并保留它们的更改历史记录。

How can I do that?

我怎样才能做到这一点?

采纳答案by VonC

The general idea is to use 'git filter-branch'and the following steps:

一般的想法是使用' git filter-branch'和以下步骤:

1) Create a submodule using --subdirectory-filter of filter-branch(after cloning your repo).

1) 使用 --subdirectory-filter of filter-branch(在克隆你的 repo 之后) 创建一个子模块。

$ git filter-branch --subdirectory-filter ABC HEAD -- --all

See this SO questionfor more on this step.

有关此步骤的更多信息,请参阅此SO 问题

2) Create a superproject using an index filter of filter-branchto delete the submodule.

2)使用索引过滤器创建一个超级项目filter-branch以删除子模块。

$ git filter-branch --index-filter "git rm -r -f --cached --ignore-unmatch ABC" --prune-empty HEAD

3) Commit the submodule to the latest version of the superproject.

3) 将子模块提交到最新版本的超级项目。

See Detach subdirectory into separate git repositoryfor a practical example.

有关实际示例,请参阅将子目录分离到单独的 git 存储库中。

Each submodule will keep its history.
But as said in this patch proposal, it would:

每个子模块将保留其历史记录。
但正如在这个补丁提案中所说,它会:

lose all the historical connections between the superproject and the submodule, breaking tools like 'git bisect', and making it difficult to recover old releases.

Ideally, each version of the newly created superproject would be linked to the correct version of the submodule (and all the .gitmodules entries would be set up correctly, too, throughout the project's history)

失去超级项目和子模块之间的所有历史联系,破坏像“ git bisect”这样的工具,并且很难恢复旧版本。

理想情况下,新创建的超级项目的每个版本都将链接到子模块的正确版本(并且所有 .gitmodules 条目也将在整个项目历史中正确设置)

If you do not need to have previous history linked to the new submodules, you can follow the steps mentioned above.
But if you do need to branch from an older point while have references to your submodules (which are currently simple sub-directories), the you could consider trying the script mentioned in the patch I refer to. It is discussed in this thread, but integrated to Git yet, as Junio C Hamano says:

如果您不需要将以前的历史链接到新的子模块,您可以按照上述步骤操作。
但是,如果您确实需要在引用子模块(当前是简单的子目录)的同时从较旧的点进行分支,则可以考虑尝试我参考的补丁中提到的脚本。这是在讨论这个线程,但集成的Git然而,当JUNIOÇ滨野说:

Unfortunately, I do not think we have designed fully (nor implemented at all) behaviour to check out different points of history that has the same submodule moved around in the superproject tree.

不幸的是,我认为我们没有完全设计(也没有实现)行为来检查在超级项目树中移动相同子模块的不同历史点。

回答by Roy Sharon

Today there's a better way of doing this: git subtree

今天有一个更好的方法来做到这一点: git subtree

See this answer.

看到这个答案