取消子模块 Git 子模块、将所有代码带回核心存储库的最佳实践是什么?
最佳答案
如果您只想将子模块代码放入主仓库,您只需删除子模块并将文件重新添加到主仓库:
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"
如果你还想保留子模块的历史,你可以做一个小技巧:将子模块“merge ”到主存储库中,这样结果就和以前一样了,除了子模块文件现在在主存储库中。
在主模块中,您需要执行以下操作:
# 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 造成任何问题。
第二个解决方案的一大优势是您仍然可以对最初位于子模块中的文件运行 git blame
或 git log
。事实上,这里发生的只是对一个存储库中的许多文件进行重命名,Git 应该会自动检测到这一点。如果您仍然遇到 git log
问题,请尝试一些选项(例如,--follow
、-M
、-C
) 可以更好地重命名和复制检测。
关于git - 如何取消子模块 Git 子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1759587/