如何合并两个不同存储库中的分支? [英] How to merge branches in 2 different repositories?

查看:93
本文介绍了如何合并两个不同存储库中的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是git的新手,我想知道如何将 Master 分支从 Sample Project 2 合并到 UAT 分支中>登台/支持.我知道可以在特定存储库下合并不同的分支,但是不确定如何合并整个存储库.

I am new to git and I would like to know how to merge Master branch from Sample Project 2 TO UAT branch in Staging/Support. I know its possible to merge different branches under a specific repository, but not sure how merging works across repositories.

我真正想做的是,在完成示例项目2 的工作后,我想将其移至 UAT 分支进行测试,等等,最后生产分支进行发布.

What I really want to do is, after finish working on Sample Project 2, I would like to move it to the UAT branch for testing etc, and eventually to the Production branch for release.

预先感谢您的帮助

推荐答案

我建议您创建暂存/支持存储库的本地克隆.然后,您将项目存储库添加为其他远程存储库,这使您可以在本地存储库中与它们进行交互.在本地进行合并,然后将结果推送到暂存库.

I would suggest you to create a local clone of the Staging/Support repository. Then you add the project repositories as further remote repositories, which allows you to interact with them in your local repository. Do the merges locally and push the result to the Staging repository.

这可以通过以下步骤实现.

This can be achieved using the following steps.

$ git clone -o staging http://staging

这将克隆您的登台存储库.您需要将" http://staging "替换为登台/支持存储库的正确URL.您可能还需要提供一个克隆存储库的路径作为另一个参数.参数-o确保将远程存储库称为暂存",这有助于稍后将其与项目存储库区分开.

This clones your staging repository. You will need to replace "http://staging" with the correct URL to your Staging/Support repository. You might also want to give a path where to clone the repository as another parameter. The parameter -o makes sure the remote repository is called "staging" which helps to distinguish it from the project repositories later on.

下一步是添加要合并的远程存储库(在本例中为示例项目2")

Next step is to add the remote repository to merge from (in this case "Sample Project 2")

$ git remote add project2 http://sampleproject2

同样,将" http://sampleproject2 "替换为存储库"Sample Project 2"的URL.您还可以更改"project2"(远程存储库的名称),以更好地适合您的项目.

Again, replace "http://sampleproject2" with the URL of the repository "Sample Project 2". You can also change "project2", which is the name of the remote repository, to better fit your project.

完成此操作后,git branch -r将显示暂存和project2的分支,如下所示:

After doing that, git branch -r will show the branches from both staging and project2, like this:

$ git branch -r
staging/Production
staging/UAT
project2/Master
project2/QA
project2/DEV

下一次签出要合并到的分支,如下所示:

Next checkout the branch you want to merge to, like this:

$ git checkout -b staging_UAT --track staging/UAT

这将创建一个名为staging_UAT的新本地分支,该分支将跟踪登台存储库中的远程分支UAT.新分支将立即检出.

This creates a new local branch called staging_UAT which tracks the remote branch UAT from the staging repository. The new branch will be checked out immediately.

现在,您已经从分期签出了UAT分支的副本.您可以合并来自project2的更改:

Now you have a copy of the UAT branch from staging checked out. You can merge in the changes from project2:

$ git merge project2/Master

现在,来自project2分支Master的所有更改都合并到当前分支(即staging_UAT)中.您可能需要看一下git log才能看到结果.如果适合您的期望,则可以将其推送到暂存库:

Now all changes from the branch Master of project2 are merged into the current branch (which is staging_UAT). You might want to have a look at git log to see the result. If it fits your expecations, you can push it to the Staging repository:

$ git push staging staging_UAT:UAT

执行此操作会将本地分支staging_UAT的当前状态推送到名为staging的远程存储库中的分支UAT.

Doing this you push the current state of your local branch staging_UAT to the branch UAT in the remote repository called staging.

您可以同等地处理其他项目,还可以添加诸如staging_Production之类的分支以将您的更改合并到Staging的Production分支中.

You can handle the other projects equally and also add a branch like staging_Production to merge your changes into the Production branch of Staging.

您可以将同一克隆用于将来的合并,而无需一次又一次地进行所有克隆和分支创建.但是,在这种情况下,您需要更新有关远程分支机构的本地信息:

You can use the same clone for future merges without doing all the cloning and branch creation again and again. However in this case you need to update your local information about the remote branches:

$ git checkout staging_UAT
$ git pull

首先,您需要更新staging_UAT以匹配Staging存储库中UAT的当前版本.这是通过拉"更改来完成的.由于分支staging_UAT是使用"--track staging/UAT"创建的,因此git知道从何处提取更改.如果分期付款的UAT从未以这种方式改变(意味着:完全使用此本地克隆从staging_UAT推送到那里),则不需要.

First you need to update staging_UAT to match the current version of UAT in the Staging repository. This is done by "pull"ing the changes. As the branch staging_UAT was created with "--track staging/UAT", git knows from where to pull the changes. If UAT on staging is never changed on any other way than this one (meaning: using exactly this local clone to push there from staging_UAT), this is not required.

如果在分期更改UAT并且您没有拉出,则在推入时会出现错误,提示:

If UAT is changed on Staging and you do not pull, you will get an error when pushing, saying:

Updates were rejected because the tip of your current branch is behind its remote counterpart.

另一个更新会影响Project2存储库:

The other update affects the Project2 repository:

$ git fetch project2

存储库project2的分支也可能已更改.如您所见,git fetch用于获取这些更改.提取和拉取之间的区别在于,拉取还将更新分支的本地副本.由于没有project2分支的本地副本,因此获取是可行的方法. (实际上git pull只是git fetchgit merge的快捷方式.)

Also the branches of the repository project2 might have been changed. As you can see git fetch is used to get those changes. The difference between fetch and pull is that pull will also update your local copy of the branch. As there is no local copy of the project2 branches, fetch is the way to go. (In fact git pull is just a shortcut for git fetch and git merge).

如果您已经具有包含代码的存储库的本地副本,则也可以将其用于合并过程.您只需要确保您不会混淆本地开发和合并,也许您需要处理更多的远程存储库,因为您有更多的开发库.

If you already have a local copy of a repository containing the code, you could also use that for the merging process. You just need to make sure you do not mix up local development and merging and maybe you need to handle more remote repositories as you have some more for your development.

本地分支staging_UAT也可以仅称为UAT,从而使推送变得更加简单(在这种情况下,git push就足够了).但是,这可能会造成混乱,因为在多个遥控器中都有使用该名称的分支.

The local branch staging_UAT could also be called just UAT, making the pushing quite simpler (git push would be enough in that case). However this might be confusing as there are branches with this name in multiple remotes.

或者,您可以更改设置" push.default ".请参阅文档,以了解这将如何影响您的推动.

Alternatively you could change your setting "push.default". See documentation to find out how that would affect your pushing.

这篇关于如何合并两个不同存储库中的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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