合并两个 Git 存储库并保留主历史记录 [英] Merge two Git repositories and keep the master history

查看:25
本文介绍了合并两个 Git 存储库并保留主历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个 Git 存储库合并到一个全新的第三个存储库中.我遵循了以下说明并且它们起作用了,但结果不是我所期望的.

母版未对齐.

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

您可以自由使用我在 GitHub 上的示例存储库:

  • 我是如何了解当前情况的:

    # 假设当前目录是我们想要创建新仓库的地方# 创建新的存储库混帐初始化# 在我们进行合并之前,我们必须有一个初始提交,所以我们将进行一个虚拟提交目录 >读.mdgit 添加.git commit -m "初始提交";# 添加远程并获取旧的 RepoAgit remote add -f RepoA https://github.com/DimitriDewaele/RepoA# 将 RepoA/master 中的文件合并到 new/mastergit merge RepoA/master --allow-unrelated-history# 对 RepoB 做同样的事情git remote add -f RepoB https://github.com/DimitriDewaele/RepoB# 将 RepoB/master 中的文件合并到 new/mastergit merge RepoB/master --allow-unrelated-history

    感谢所有帮助!

    更新:答案

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

    • 解决方案

      很简单,不用合并使用 rebase.假设你想要 repoA/master 首先只是做(在获取和添加遥控器之后)

      # 从 master 开始git rebase RepoB/master # 将 master 放在 masterB 之上git rebase RepoA/master # 将 master (with masterB) 放在 masterA 之上

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

      I need to merge two Git repositories into a brand new, third repository. I followed the following instructions and they work, but the result is not what I expected. Merge two Git repositories without breaking file history

      Look at this:

      The master is not aligned.

      • Is it possible to merge everything onto the same master?
      • I would like to have a clean history on the master.

      You are free to use my example repository's on GitHub:

      This is my merged result.

      This is the situation that I would like to have: (painted solution!!!)

      How I got to the the current situation:

      # 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
      

      All help is appreciated!

      UPDATE: ANSWER

      The correct answer is to rebase, instead of merge.

      Code:

      # 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
      

      One problem remains. The 2nd repo loses the parent view. I posted a follow-up question for this: Merge two Git repos and keep the history

      解决方案

      It's easy, instead of merging use rebase. Assuming you want to have repoA/master first just do (after fetching and adding remotes)

      # 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
      

      Note that rebases are potentially destructive and a bit unintuitive at first, so I strongly recommend reading about them first (SO documentation in the link above is a good place to start)

      这篇关于合并两个 Git 存储库并保留主历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆