【问题标题】:GIT MERGE a repo with a folder in another repo without loosing historyGIT MERGE 将一个 repo 与另一个 repo 中的文件夹合并,而不会丢失历史记录
【发布时间】:2019-12-27 15:31:30
【问题描述】:

我有两个存储库,假设是 Repo A 和 Repo B,其结构如下所示:

Repo A (there are many files in each repo. I am just showing 2 files for example):
  |
  |
  |---Test1.cs  (It has some changes made by X Developer)
  |---Test2.cs  (It has some changes made by X Developer)

Repo B:
  |
  |
  |---src
       |
       |
       |---Test1.cs  (It has some changes made by Y Developer)
       |---Test2.cs  (It has some changes made by Y Developer)

我想在不丢失历史记录的情况下将文件从 Repo A 合并(或变基)到 Repo B/src。合并后,当我查看历史记录时,我想查看 Developer X 和 Y 的更改。 这可能吗?如果是,请指导我如何执行此操作。

我查看了其他 SO 帖子并尝试添加远程仓库...等。但这些并未涵盖这种情况。我的 GIT 版本是 2.21.0。

【问题讨论】:

  • 如果您尝试将repo A 作为远程添加到repo B,您是否尝试过使用此标志--allow-unrelated-histories
  • 这能回答你的问题吗? Merge git repository in subdirectory
  • @SaurabhPBhandari 谢谢你的链接。是的,我尝试添加为远程仓库并使用了您共享的 SO 链接中列举的所有步骤。它们就像替换文件,但不像在同一个 repo 中合并分支。
  • 历史,在 Git 存储库中,只是存储库中的一组提交。只要您没有从存储库中删除提交,您就不会丢失任何历史记录。您最有可能看到的问题是您询问的是名为 /Test1.csfile 与名为 /src/Test1.csfile 的历史,除非您可以让 Git 将这些视为 same 文件,好吧,例如,每当您选择“仅那些包含 src/Test1.cs 的提交”时,您都将从该视图中丢弃所有那些提交也包含“Test1.cs”(不在“src/”中)。
  • @torek 感谢您的评论。有没有办法告诉 GIT 将它们视为相同的文件?

标签: git merge rebase remote-repository


【解决方案1】:

您可以为此使用普通合并,但您必须使用merge --allow-unrelated-histories 来允许合并不相关的历史记录。

例如:

cd target-repository
# we work on master
git checkout master
# add the repository as a remote
git remote add other /path/to/source-repo
# fetch the remote repository, which will create other/master
git fetch other
# merge the histories, specifiy --allow-unrelated-histories to avoid the related history check
git merge --allow-unrelated-histories other/master

这假设文件不重叠。

【讨论】:

  • 谢谢,但正如帖子中所解释的,在我的情况下文件重叠,这就是这些步骤对我没有帮助的原因。
  • 如果它们重叠,您可以进行正常合并。如果您有再次出现的冲突,请使用git rerere 记录相应的解决方案。
  • 我试过这个选项。但它说它已合并,但既没有看到合并冲突,也没有看到目标文件中的开发人员更改。很抱歉,我无法获得 rerere 命令。它是什么,你能给我举个例子吗?
猜你喜欢
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 2014-02-16
  • 2021-06-19
  • 1970-01-01
  • 2013-01-28
  • 2010-11-24
  • 2016-08-01
相关资源
最近更新 更多