合并两个git存储库,保留所有提交历史记录

时间:2011-05-09 21:43:37

标签: git

我有两个git repos,每个都包含相同代码库的不同版本。

在回购1中提交(最近一次):

 version 1
 version 2
 version 3
 version 4
 version 5

在回购2中提交:

 version 3
 commit that isn't a new version
 another commit that isn't a new version
 yet another commit that isn't a new version
 version 5

请注意,在Repo 1中,版本5基于版本4,但在Repo 2中,版本4不存在,因为版本5实际上基于版本3(并且版本4成为废弃的分支,基本上)。

我想把所有这些都放到一个回购中:

 version 1
 version 2
 version 3
   branch  version 4
 commit that isn't a new version
 another commit that isn't a new version
 yet another commit that isn't a new version
 version 5

如果有人不仅可以解释 如何做到这一点,我会感激,但为什么这样做是正确的,所以我可以更好地理解git。

1 个答案:

答案 0 :(得分:1)

看看git rebase --preserve-merges --onto。在将第二个仓库的提交提取到第一个仓库后,您可以移动历史记录部分以获得所需的祖先。

希望这有帮助。

编辑:

要从第二个回购中获取更改,请将其添加为远程:

git remote add secondary <url to your secondary repo>

然后你可以从那里取货:

git fetch secondary

现在,您可以使用gitk --allgit log --all --graph --decorate --oneline进行检查,并使用rebase --onto执行您喜欢的操作。