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

查看:412
本文介绍了如何取消对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 blame或git log.实际上,您在这里所做的是在一个存储库中重命名许多文件,而git应该自动检测到这一点.如果您仍然在git log上遇到问题,请尝试使用某些选项(--follow,-M,-C),它们可以更好地重命名/复制检测.

In this second solution you will have the big advantage that you can still run git blame or git log on the files which were originally in submodules. In fact what you did here is to rename many files inside one repository, and git should autodetect this. If you still have problems with git log, try some options (--follow, -M, -C) which do better rename/copy detection.

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

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