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


当前回答

我发现从子模块获取本地提交数据更方便,否则我就会丢失它们。(无法推送,因为我没有遥控器)。所以我添加了submodule/。Git为remote_origin2,从该分支中提交并合并。 不确定我是否仍然需要子模块远程作为起源,因为我对git还不够熟悉。

其他回答

我发现从子模块获取本地提交数据更方便,否则我就会丢失它们。(无法推送,因为我没有遥控器)。所以我添加了submodule/。Git为remote_origin2,从该分支中提交并合并。 不确定我是否仍然需要子模块远程作为起源,因为我对git还不够熟悉。

在主回购

Git rm——缓存[submodules_repo] git commit -m "Submodules removed." Git push origin [master]

在子模块repo

Rm -rf .git

还是主回购

Git add [submodules_repo] Git添加。 Submodules repo被添加到main中。 Git push origin [master]

我找到的最佳答案是:

http://x3ro.de/2013/09/01/Integrating-a-submodule-into-the-parent-repository.html

这篇文章很好地解释了这个过程。

如果你只是想把你的子模块代码放到主库中,你只需要删除子模块,并将文件重新添加到主库中:

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"

如果还想保留子模块的历史,可以使用一个小技巧:将子模块“合并”到主存储库中,这样结果将与以前相同,只是子模块文件现在位于主存储库中。

在主模块中,你需要做以下工作:

# 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日志的问题,尝试一些选项(例如,——follow, -M, -C),它们可以更好地重命名和复制检测。

Git rm——缓存了the_submodule_path 从.gitmodules文件中删除子模块部分,或者如果它是唯一的子模块,则删除该文件。 执行一个提交"removed submodule xyz" Git添加了_submodule_path 另一次提交“添加xyz的代码库”

我还没找到更简单的方法。你可以通过git commit -a把3-5压缩到一个步骤中——这取决于你的喜好。