git - 如何取消子模块 Git 子模块?

标签 git git-submodules

取消子模块 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 blamegit log。事实上,这里发生的只是对一个存储库中的许多文件进行重命名,Git 应该会自动检测到这一点。如果您仍然遇到 git log 问题,请尝试一些选项(例如,--follow-M-C) 可以更好地重命名和复制检测。

关于git - 如何取消子模块 Git 子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1759587/

相关文章:

.net - NuGet 和 Git 子模块

git - GIT嵌套存储库:Composer与SubModules与Subtree与?

python - 如何将 git diff 结果与代码行号并排输出以嵌入网页?

git - pip 从 git repo 分支安装/使用子模块提交

git - 我怎样才能忽略 TortoiseGit blame 中的空白更改

bash - git 子模块 foreach 与 awk

git - 在这种情况下如何使用 Git 处理共享代码?

git - Jenkins,通过 ssh 与多个用户获取 git 子模块?

git - 如何从一个分支推送到另一个分支并 checkout ?

git - 尝试部署到 github 分支上的 firebase