git 将一个拉取请求合并到多个分支中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21441021/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:39:05  来源:igfitidea点击:

Merging a pull request into multiple branches

gitgithub

提问by Salil Surendran

We have pull requests coming from other forks as well as branches within our own fork that has to go to multiple branches. Is there a way we can merge pull requests to multiple branches in one step?

我们有来自其他分叉以及我们自己分叉中的分支的拉取请求,这些请求必须转到多个分支。有没有一种方法可以一步将拉取请求合并到多个分支?

回答by bitoiu

No, you can't unfortunately: http://git-scm.com/docs/git-merge

不,不幸的是你不能:http: //git-scm.com/docs/git-merge

When I have that requirement (which is not that often), I usually do it manually in the console. If that is a big burden on your workflow I would suggest a shell-script that could first do all the merges without conflicts and then let you manually fix the others.

当我有这个要求时(这并不常见),我通常在控制台中手动完成。如果这对您的工作流程造成很大负担,我建议使用一个 shell 脚本,它可以首先在没有冲突的情况下进行所有合并,然后让您手动修复其他合并。

Is this a requirement because you have too many concurrent development lines at the same time?

这是一个要求,因为您同时有太多的并发开发线吗?

回答by Kiarash Zamanifar

This should have been implemented into GitHub to make sure commits into release branches are also merged into master.

这应该已经在 GitHub 中实现,以确保提交到发布分支也被合并到 master 中。

Plugnins (3rd party apps) could help but not for free. https://github.com/marketplace/mergify

插件(第 3 方应用程序)可以提供帮助,但不是免费的。 https://github.com/marketplace/mergify

回答by seato

Not in one step, but one workaround:

不是一步,而是一种解决方法:

I'm going to assume the branch that your pull request is based off of (and all of the other branches that you want to merge the pr into) are rebased against master. If not then rebase all of them with:

我将假设您的拉取请求所基于的分支(以及您想要将 pr 合并到的所有其他分支)针对 master 重新定位。如果没有,则将所有这些重新设置为:

$ git rebase origin/master

What we're going to do is take the diff of your pull request, stash it, and then apply the stash to multiple branches. To make this easier, I'd recommend squashing your commitsfirst. Once you squash them, we're going to undo your last commit (the changes will remain since we're using the softflag, but we want to hold on to the changes in our stash).

我们要做的是获取您的拉取请求的差异,存储它,然后将存储应用到多个分支。为了使这更容易,我建议先压缩您的提交。一旦您压缩它们,我们将撤消您的最后一次提交(由于我们使用了soft标志,因此更改将保留,但我们希望保留我们存储中的更改)。

$ git reset --soft HEAD^

Now we want to reapply them to this branch:

现在我们想将它们重新应用到这个分支:

$ git stash apply

Note that I used applyand not pop, this ensures that the changes are not dropped. Switch over to your other branches and apply the stash on those as well and commit the changes. Note: When you commit, you're going to have different commit SHA's (because those are all different changes applied to different branches). What this means is you're going to have conflicts if those branches ever come to merge, but hopefully it's easy since the changes should be the same. That being said, I definitely don't recommend this route. Here's what I recommend.

请注意,我使用了apply而 not pop,这可确保不会删除更改。切换到您的其他分支并在这些分支上应用 stash 并提交更改。注意:当您提交时,您将拥有不同的提交 SHA(因为这些都是应用于不同分支的不同更改)。这意味着如果这些分支合并,您将发生冲突,但希望这很容易,因为更改应该是相同的。话虽如此,我绝对不推荐这条路线。这是我推荐的。

You should be merging that change onto the master branch and rebasing the other branches. Life's so much easier if you do that.

您应该将该更改合并到 master 分支并重新设置其他分支。如果你这样做,生活就会容易得多。