合并两个Git存储库并保留主历史记录

Dim*_*ele 5 git merge master

我需要将两个Git存储库合并到一个全新的第三个存储库中.我按照以下说明操作,但结果不符合我的预期.在不破坏文件历史记录的情况下合并两个Git存储库

看这个:

Git Merge操作

主人没有被分配.

  • 是否可以将所有内容合并到同一个主服务器上?
  • 我想对主人有一个干净的历史.

您可以在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
Run Code Online (Sandbox Code Playgroud)

所有帮助表示赞赏!

更新:答案

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

码:

# 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
Run Code Online (Sandbox Code Playgroud)

还有一个问题.第二个回购失去父视图.我发布了一个跟进问题:合并两个Git回购并保留历史记录

在此输入图像描述

Dun*_*nno 3

这很简单,不用合并使用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
Run Code Online (Sandbox Code Playgroud)

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