Git将子模块合并到父树中,并保留提交历史记录

Ala*_*rte 15 git git-submodules

我有一个包含两个子模块的存储库,我想将其转换为单个项目.许多答案涉及脚本,有些似乎过于复杂.

[submodule "site"]
    path = wp-content/themes/site
    url = https://ajf-@bitbucket.org/ajf-/site.git
    fetchRecurseSubmodules = true
    ignore = all
[submodule "wpsite"]
    path = wp-content/themes/wpsite
    url = https://ajf-@bitbucket.org/ajf-/wpsite.git
    fetchRecurseSubmodules = true
    ignore = all
Run Code Online (Sandbox Code Playgroud)

是否有正式支持/记录的方法将这些子模块合并到父存储库中?

Sig*_*igi 22

最好的方法是进行子树合并.

首先,从超级项目中删除子模块和相关配置; 编辑.gitmodules文件以删除受影响的子模块,或者如果要合并所有子模块,则完全删除该文件.也删除子模块目录.

接下来,将子模块存储库添加为超级项目的正确远程控制器:

git remote add site https://ajf-@bitbucket.org/ajf-/site.git
git remote add wpsite https://ajf-@bitbucket.org/ajf-/wpsite.git
Run Code Online (Sandbox Code Playgroud)

然后,获取遥控器:

git fetch --all
Run Code Online (Sandbox Code Playgroud)

现在,从每个子项目中查看要移植到主项目的分支:

git checkout -b site-branch site/some_branch
git checkout -b wpsite-branch wpsite/some_other_branch
Run Code Online (Sandbox Code Playgroud)

您现在已准备好将这些分支合并为主项目的子树:

git read-tree --prefix=site/ -u site-branch
git read-tree --prefix=wpsite/ -u wpsite-branch
Run Code Online (Sandbox Code Playgroud)

而且你已经完成了.检查结果gitk --all.

由于您希望转换为单个项目,因此您不会单独更新子项目,因此我不打算描述其工作原理.

您可以在Pro Git中关于子树合并章节中阅读此内容

  • 请注意,这个答案(至少对我而言)不会保留子模块的历史记录。 (3认同)
  • 我想你应该在read-tree步骤之前添加`git checkout master` (2认同)

rye*_*nus 10

聚会有点晚了,但对于那些仍在寻求帮助的人,请查看以下内容:

  1. https://gitirc.eu/howto/using-merge-subtree.html
  2. https://help.github.com/en/github/using-git/about-git-subtree-merges

以下是第一篇文章的逐字副本:

1. git remote add -f Bproject /path/to/B
2. git merge -s ours --no-commit --allow-unrelated-histories Bproject/master
3. git read-tree --prefix=dir-B/ -u Bproject/master
4. git commit -m "Merge B project as our subdirectory"

5. git pull -s subtree Bproject master
Run Code Online (Sandbox Code Playgroud)

解释:

1. name the other project "Bproject", and fetch.
2. prepare for the later step to record the result as a merge.
3. read "master" branch of Bproject to the subdirectory "dir-B".
4. record the merge result.

5. pull in subsequent update from Bproject using "subtree" merge
Run Code Online (Sandbox Code Playgroud)

作为第 4 步之前的替代方法,您可能想要更新.gitmodules文件,或者只是将其删除:

3.1 git rm --cached .gitmodules
Run Code Online (Sandbox Code Playgroud)

这样子模块的历史就得到了很好的保存。