【问题标题】:Merge two Git repositories and keep the master history合并两个 Git 存储库并保留主历史记录
【发布时间】:2017-06-28 22:49:09
【问题描述】:

我需要将两个 Git 存储库合并到一个全新的第三个存储库中。 我按照以下说明进行操作,它们可以正常工作,但结果不是我所期望的。 Merge two Git repositories without breaking file history

看看这个:

母版未对齐。

  • 是否可以将所有内容合并到同一个母版上?
  • 我想对主人有一个干净的历史记录。

您可以免费使用我的示例存储库在 GitHub 上

这是我的合并结果

这是我想要的情况:(画出解决方案!!!)

我是如何走到现在的:

# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init

# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
dir > Read.md
git add .
git commit -m "initial commit"

# Add a remote for and fetch the old RepoA
git remote add -f RepoA https://github.com/DimitriDewaele/RepoA

# Merge the files from RepoA/master into new/master
git merge RepoA/master --allow-unrelated-histories

# Do the same thing for RepoB
git remote add -f RepoB https://github.com/DimitriDewaele/RepoB

# Merge the files from RepoB/master into new/master
git merge RepoB/master --allow-unrelated-histories

感谢所有帮助!

更新:答案

正确的答案是变基,而不是合并。

代码:

# Rebase the working branch (master) on top of repoB
git rebase RepoB/master

# Rebase teh working branch (master with RepoB) on top op repoA
git rebase RepoA/master

还有一个问题。第二个 repo 失去了父视图。我为此发布了一个后续问题:Merge two Git repos and keep the history

【问题讨论】:

  • 您能否更详细地解释您的预期以及有何不同?
  • 您在一个主机上拥有一切,我不确定您所说的“对齐”是什么意思 - 尝试从本地删除 RepoA 和 RepoB 遥控器,然后查看图表的样子。
  • 我添加了一个“涂漆解决方案”

标签: git merge master


【解决方案1】:

这很简单,而不是合并使用rebase。假设您想首先拥有repoA/master(在获取和添加遥控器之后)

# starting on master
git rebase RepoB/master # place master on top of masterB
git rebase RepoA/master # place master (with masterB) on top of masterA

请注意,变基可能具有破坏性并且一开始有点不直观,所以我强烈建议先阅读它们(上面链接中的 SO 文档是一个很好的起点)

【讨论】:

  • 感谢您的正确回答。一个小问题:RepoA 的变基让我失去了合并历史。见上图(我在上面添加了当前状态)。可以保留这个吗?
  • @DimitriDewaele 试试rebase--preserve-merges 选项
  • 正如@DimitriDewaele 的后续问题所述,rebase 确实不是您想要使用的:stackoverflow.com/a/42457384/54249
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2012-10-14
相关资源
最近更新 更多