git 如何将来自两个不同存储库的两个分支合并到一个存储库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/244695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine two branches from two different repositories in a single repository?
提问by Christoph Schiessl
The structures of my Git repositories look like this:
我的 Git 存储库的结构如下所示:
A-B-C-D-E-F # master branch in separate repo1
A-B-C-D-E-G-H # master branch in separate repo2
A-H are simple commits. As you can see the repositories are related (repo2 is a fork of repo1). I'm trying to combine these two repositories in one.
AH 是简单的提交。如您所见,存储库是相关的(repo2 是 repo1 的一个分支)。我正在尝试将这两个存储库合二为一。
Afterwards the single repository should have the following structure:
之后,单个存储库应具有以下结构:
A-B-C-D-E-F # master branch of previous repo1
\
\
G-H # master branch of previous repo2
I've already spent a lot of time reading the Git User's Guide and so on. However, this (special) case of use doesn't seem to be documented anywhere.
我已经花了很多时间阅读 Git 用户指南等等。但是,这种(特殊)使用情况似乎没有记录在任何地方。
回答by Peter Burns
You can treat another git repository on the same filesystem as a remote repo.
您可以将同一文件系统上的另一个 git 存储库视为远程存储库。
In the first, do the following:
首先,执行以下操作:
git remote add <name> /path/to/other/repo/.git
git fetch <name>
git branch <name> <name>/master #optional
Now they're both branches in a single repository. You can switch between them with git checkout, merge with git merge, etc.
现在它们都是单个存储库中的分支。您可以使用 git checkout、与 git merge 合并等在它们之间切换。
回答by Jim Puls
Your picture suggests that you don't really want to "combine the two repositories" so much as merge commits G and H in to repo1. You should be able to do something as simple as add repo2 as a remote to repo1 and fetch/pull the changes in.
您的图片表明您真的不想“合并两个存储库”,因为将 G 和 H 合并到 repo1 中。您应该能够做一些简单的事情,例如将 repo2 作为远程添加到 repo1 并获取/拉取更改。
回答by Pistos
I think Jim is right. Also bear in mind that if two commits do exactly the same patch/diff against the code, they will have exactly the same SHA1 hash. So A through E should have the same SHA1 in both repos, so it shouldn't be a problem to merge from one to the other, and keep the merged repo as the sole repo to move forward with.
我认为吉姆是对的。还请记住,如果两次提交对代码执行完全相同的补丁/差异,则它们将具有完全相同的 SHA1 哈希值。因此 A 到 E 在两个存储库中应该具有相同的 SHA1,因此从一个到另一个合并应该没有问题,并将合并的存储库作为唯一的存储库继续前进。
You can setup on repo1 a tracking branch for repo2, and then keep them distinct that way, and delay the merge to whenever you want.
您可以在 repo1 上为 repo2 设置一个跟踪分支,然后以这种方式保持它们不同,并在需要时延迟合并。