【问题标题】:Git merge submodule into parent tree cleanly and preserving commit historyGit 将子模块干净地合并到父树中并保留提交历史记录
【发布时间】:2014-04-27 19:06:03
【问题描述】:

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

[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

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

【问题讨论】:

标签: git git-submodules


【解决方案1】:

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

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

接下来,将子模块存储库作为适当的遥控器添加到您的超级项目中:

git remote add site https://ajf-@bitbucket.org/ajf-/site.git
git remote add wpsite https://ajf-@bitbucket.org/ajf-/wpsite.git

然后,获取遥控器:

git fetch --all

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

git checkout -b site-branch site/some_branch
git checkout -b wpsite-branch wpsite/some_other_branch

之后,返回master 分支,或者返回要创建组合超级项目的分支:

git checkout master

如果您想为操作创建一个(可能是临时的)额外分支,请改为:

git checkout -b new-superproject master

您现在已准备好将模块分支作为 子树 与您的主项目(在此示例中的 master 分支中)合并:

git read-tree --prefix=site/ -u site-branch
git read-tree --prefix=wpsite/ -u wpsite-branch

你就完成了。使用gitk --all 检查结果。

由于您要转换为单个项目,因此您不会独立更新子项目,因此我不打算描述它是如何工作的。

您可以在chapter on subtree merging from Pro Git 中阅读此内容

【讨论】:

【解决方案2】:

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

  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

解释:

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

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

3.1 git rm --cached .gitmodules

这样子模块的历史就被很好地保存了。

【讨论】:

  • 这是一个很好的答案。我应该更新我的答案以反映您添加 merge 步骤以保留历史记录的建议。
猜你喜欢
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2021-12-25
  • 2015-04-17
  • 1970-01-01
  • 2013-06-29
相关资源
最近更新 更多