【问题标题】:Can I merge two Git repositories with similar content, but not sharing any ancestry?我可以合并两个内容相似但不共享任何祖先的 Git 存储库吗?
【发布时间】:2011-08-14 02:57:10
【问题描述】:

我有两个代码库,Code base-1,Code Base-2。

我运行命令...

git init
git add .
git commit -m "Initial Commit"

...在两个存储库(目录)中。

code base-1 和 code base-2 之间存在代码差异。

我无法对它们进行分支,因为它们已经包含大约 0.1% 的差异。文件名相同,但代码略有修改。

有没有办法合并两个存储库之间的差异?

已编辑:

例如,假设我有以下内容。这就是我要开始的。代码库 1 和 2 之间存在细微差别。

[oldest code case] 
code-base-1/ 
code-base-1/.git  [git stuff with already created repo] 
code-base-1/file1 
code-base-1/file2 

code-base-2/ 
code-base-2/.git [git stuff with already created repo] 
code-base-2/file1 
code-base-2/file2 

理想情况下,我可以删除 code-base-2,因为它有点新。

我将如何合并这些代码库,以便最终得到一个包含合并文件的代码库?

【问题讨论】:

    标签: git merge


    【解决方案1】:

    问题可能是因为它们是两个不同的存储库(具有不同的 SHA-1 第一个节点),而不是一个克隆然后修改的存储库。
    这意味着 'git fetch' 可能还不够。

    您可以简单地使用外部工具(即 Git 外部)将 CB2(Code-Base-2)的 内容 合并到 CB1 中,如问题“@987654321”中建议的 WinMerge @"。

    另一种选择是使用某种移植技术来完成提取:请参阅问题“Can two identically structured git repos be merged when they have never had any common history?

    $ cd project1
    $ git config remote.project2.url /path/to/project2
    $ git config remote.project2.fetch 'refs/heads/*:refs/project2/*'
    $ git fetch project
    

    另外修改graft文件(.git/info/grafts),将project2的提交链接到project1。

    【讨论】:

      【解决方案2】:

      试试:

      cd code-base-1
      git fetch ../code-base-2 master:code-base-2
      git merge code-base-2
      

      【讨论】:

      • 这将加入同一项目的两个版本,就像它们是不同的项目一样(如果它们没有共同的课程提交)。
      【解决方案3】:

      按照您的描述创建第一个存储库。

      在第二个文件夹中创建一个名为“.git”的文件,其内容为:

      gitdir: <path-to-first-repository>/.git
      

      在第二个存储库中,您现在可以运行 git diff、git add/commit 等来合并您的更改。

      【讨论】:

      • 可以更具体一点。例如,假设我有以下内容。这就是我要开始的。代码库 1 和 2 之间存在细微差别。 [最旧的代码案例] code-base-1/ code-base-1/.git [git stuff with already created repo] code-base-1/file1 code-base-1/ file2 code-base-2/code-base-2/.git [git stuff with already created repo] code-base-2/file1 code-base-2/file2 理想情况下,我可以删除 code-base-2,因为它是有点新。我将如何合并这些代码库。所以我最终会拿出一个包含合并文件的文件。
      猜你喜欢
      • 2019-11-23
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 2012-08-23
      • 1970-01-01
      • 2012-04-29
      • 2017-12-13
      相关资源
      最近更新 更多