如何取消子模块 Git 子模块? [英] How to un-submodule a Git submodule?

查看:113
本文介绍了如何取消子模块 Git 子模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取消对 Git 子模块的子模块化,将所有代码带回核心存储库的最佳实践是什么?

What are the best practices for un-submoduling a Git submodule, bringing all the code back into the core repository?

推荐答案

如果您只想将子模块代码放入主存储库,则只需删除子模块并将文件重新添加到主存储库中:

If all you want is to put your submodule code into the main repository, you just need to remove the submodule and re-add the files into the main repo:

git rm --cached submodule_path # delete reference to submodule HEAD (no trailing slash)
git rm .gitmodules             # if you have more than one submodules,
                               # you need to edit this file instead of deleting!
rm -rf submodule_path/.git     # make sure you have backup!!
git add submodule_path         # will add files instead of commit reference
git commit -m "remove submodule"

如果你还想保留子模块的历史,你可以做一个小技巧:将子模块合并"到主存储库中,这样结果就会和以前一样,只是子模块文件现在在主存储库中.

If you also want to preserve the history of the submodule, you can do a small trick: "merge" the submodule into the main repository, so that the result will be the same as it was before, except that the submodule files are now in the main repository.

在主模块中,您需要执行以下操作:

In the main module you will need to do the following:

# Fetch the submodule commits into the main repository
git remote add submodule_origin git://url/to/submodule/origin
git fetch submodule_origin

# Start a fake merge (won't change any files, won't commit anything)
git merge -s ours --no-commit submodule_origin/master

# Do the same as in the first solution
git rm --cached submodule_path # delete reference to submodule HEAD
git rm .gitmodules             # if you have more than one submodules,
                               # you need to edit this file instead of deleting!
rm -rf submodule_path/.git     # make sure you have backup!!
git add submodule_path         # will add files instead of commit reference

# Commit and cleanup
git commit -m "removed submodule"
git remote rm submodule_origin

生成的存储库看起来有点奇怪:将有多个初始提交.但是不会对Git造成任何问题.

The resulting repository will look a bit weird: there will be more than one initial commit. But it won’t cause any problems for Git.

第二种解决方案的一大优点是您仍然可以对最初位于子模块中的文件运行 git blamegit log.事实上,这里发生的只是对一个存储库中的许多文件进行重命名,Git 应该会自动检测到这一点.如果您仍然对 git log 有问题,请尝试一些选项(例如,--follow-M-C),它可以更好地重命名和复制检测.

A big advantage of this second solution is that you can still run git blame or git log on the files which were originally in submodules. In fact, what happens here is just a renaming of many files inside one repository, and Git should automatically detect this. If you still have problems with git log, try some options (e.g., --follow, -M, -C) which do better rename and copy detection.

这篇关于如何取消子模块 Git 子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆