如何在Git仓库中合并两个并行分支?

问题描述 投票:1回答:1

我有一个没有Git存储库的项目,因此我在Gitlab中为此创建了一个存储库。在创建存储库时,我有两个文件夹,一个包含生产代码,另一个包含开发代码。

我在包含生产代码的文件夹中创建了本地git存储库。我将代码推送到最初创建的存储库的远程master

然后,我在包含开发代码的文件夹中创建了本地git存储库。我从本地管理员创建了一个新的本地分支(假设为xyz)。我将此新的本地分支xyz推送到了相同的远程存储库。

远程存储库现在具有master分支和xyz分支。现在,在完成开发后,我想将xyz分支合并到master

但是当我试图变得过时时,我在同一存储库中有两个并行的分支,它们具有无关的历史记录。一种解决方案是删除存储库,然后以我刚刚完成的新代码重新开始,但是有什么办法可以保留旧代码并合并这些分支?

git gitlab git-merge
1个回答
0
投票

如果我理解正确,那么在上一个生产环境中,您在master中只有一个修订版本(或者可能有多个修订版本,都没关系)...。那么从第一个生产环境开始,您就有一个与master平行的分支修订版是大师的延续。那正确吗?因此,如果我们能够将xyz的第一个(最旧的)修订版(该修订版的内容)放在主版本上,然后在xyz的第一个修订版之后重播所有其他修订版,您会感到高兴吗?

好吧,如果是这样:

git checkout first-revision-xyz # check out the first revision of xyz
git reset --soft master # set the branch pointer on top of the current position of master (master won't be touched, don't worry)
git commit --amend --no-edit # create a new revision, comment will be the same of the first revision of xyz)
git cherry-pick first-revision-of-xyz..xyz # replay all the other revisions up to the last revision of xyz
# if you like the result
git checkout -b development

您在其中创建了分支开发,紧随master之后,您可以照常开始使用它。

作为另一种策略,您可能希望合并两个分支,但要保留树(文件和内容)在xyz上的状态?无需经过真正的合并过程即可完成。

git checkout -b test $( git commit-tree -p master -p xyz -m "Merge of prod an development" ${xyz^tree} )

Created a test branch that has the merge of both branches keeping the tree as it is on xyz.
© www.soinside.com 2019 - 2024. All rights reserved.