git 如何合并两个不同存储库中的分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38468700/
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 merge branches in 2 different repositories?
提问by user3528213
I am new to git and I would like to know how to merge Masterbranch from Sample Project 2TO UATbranch in Staging/Support. I know its possible to merge different branches under a specific repository, but not sure how merging works across repositories.
我是 git 的新手,我想知道如何将Master分支从Sample Project 2合并到Staging/Support 中的UAT分支。我知道可以在特定存储库下合并不同的分支,但不确定跨存储库合并是如何工作的。
What I really want to do is, after finish working on Sample Project 2, I would like to move it to the UATbranch for testing etc, and eventually to the Productionbranch for release.
我真正想做的是,在完成Sample Project 2 的工作后,我想将其移至UAT分支进行测试等,并最终移至Production分支进行发布。
Thanks in advance for the help
在此先感谢您的帮助
回答by lucash
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.
我建议您创建暂存/支持存储库的本地克隆。然后您将项目存储库添加为进一步的远程存储库,这允许您在本地存储库中与它们进行交互。在本地进行合并并将结果推送到 Staging 存储库。
This can be achieved using the following steps.
这可以使用以下步骤来实现。
Initialization and first merge
初始化和第一次合并
$ git clone -o staging http://staging
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.
这会克隆您的临时存储库。您需要将“ http://staging”替换为您的 Staging/Support 存储库的正确 URL。您可能还想提供克隆存储库的路径作为另一个参数。参数 -o 确保远程存储库被称为“暂存”,这有助于稍后将其与项目存储库区分开来。
Next step is to add the remote repository to merge from (in this case "Sample Project 2")
下一步是添加要从中合并的远程存储库(在本例中为“示例项目 2”)
$ git remote add project2 http://sampleproject2
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.
同样,将“ http://sampleproject2”替换为存储库“Sample Project 2”的 URL。您还可以更改“project2”,即远程存储库的名称,以更好地适应您的项目。
After doing that, git branch -r
will show the branches from both staging and project2, like this:
这样做之后,git branch -r
将显示来自 staging 和 project2 的分支,如下所示:
$ 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
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.
这将创建一个名为 staging_UAT 的新本地分支,它从临时存储库跟踪远程分支 UAT。新分支将被立即签出。
Now you have a copy of the UAT branch from staging checked out. You can merge in the changes from project2:
现在,您已检出暂存的 UAT 分支副本。您可以合并来自 project2 的更改:
$ git merge project2/Master
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:
现在,project2 的分支 Master 的所有更改都合并到当前分支(即 staging_UAT)中。您可能想git log
查看一下结果。如果它符合您的期望,您可以将其推送到 Staging 存储库:
$ git push staging staging_UAT: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_UAT 的当前状态推送到名为 staging 的远程存储库中的分支 UAT。
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.
您可以平等地处理其他项目,也可以添加一个像 staging_Production 这样的分支来将您的更改合并到 Staging 的 Production 分支中。
Future merges
未来合并
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
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.
首先,您需要更新 staging_UAT 以匹配 Staging 存储库中的当前 UAT 版本。这是通过“拉”更改来完成的。由于分支 staging_UAT 是使用“--track staging/UAT”创建的,因此 git 知道从何处提取更改。如果暂存上的 UAT 从未以除此之外的任何其他方式更改(意思是:正是使用此本地克隆从 staging_UAT 推送到那里),则不需要这样做。
If UAT is changed on Staging and you do not pull, you will get an error when pushing, saying:
如果在 Staging 上更改了 UAT 而您不拉,则推时会出现错误,说:
Updates were rejected because the tip of your current branch is behind its remote counterpart.
The other update affects the Project2 repository:
另一个更新影响 Project2 存储库:
$ git fetch project2
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
).
此外,存储库 project2 的分支也可能已更改。如您所见git fetch
,用于获取这些更改。fetch 和 pull 的区别在于 pull 也会更新你的本地分支副本。由于没有 project2 分支的本地副本,所以 fetch 是要走的路。(实际上git pull
只是git fetch
and的快捷方式git merge
)。
Adaptions
改编
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.
如果您已经拥有包含代码的存储库的本地副本,您也可以将其用于合并过程。您只需要确保不要混淆本地开发和合并,也许您需要处理更多远程存储库,因为您有更多用于开发。
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.
本地分支 staging_UAT 也可以称为 UAT,使推送变得非常简单(git push
在这种情况下就足够了)。但是,这可能会令人困惑,因为在多个遥控器中存在具有此名称的分支。
Alternatively you could change your setting "push.default". See documentation to find out how that would affect your pushing.
或者,您可以更改设置“ push.default”。请参阅文档以了解这将如何影响您的推送。