git 在 Github 中将拉取请求合并到与默认不同的分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9135913/
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
Merge pull request to a different branch than default, in Github
提问by eoinoc
A pull request comes into my repo hosted on Github. By default it is merged into the master
branch.
拉取请求进入我托管在 Github 上的存储库。默认情况下,它会合并到master
分支中。
Is there any way for me to change which branch the changes would be merged into?
有什么方法可以让我更改更改将合并到哪个分支?
采纳答案by maliayas
As of 15.08.2016GitHub allows changing the target branch of a pull request via the GUI. Click Edit
next to the title, then select the branch from the dropdown.
截至 2016 年 8 月 15 日,GitHub 允许通过 GUI 更改拉取请求的目标分支。单击Edit
标题旁边的,然后从下拉列表中选择分支。
You can now change the base branch of an open pull request. After you've created a pull request, you can modify the base branch so that the changes in the pull request are compared against a different branch. By changing the base branch of your original pull request rather than opening a new one with the correct base branch, you'll be able to keep valuable work and discussion.
您现在可以更改开放拉取请求的基本分支。创建拉取请求后,您可以修改基本分支,以便将拉取请求中的更改与不同的分支进行比较。通过更改原始拉取请求的基本分支而不是使用正确的基本分支打开一个新分支,您将能够保留有价值的工作和讨论。
回答by Daniel Pittman
The submitter can change that when they issue the pull request, but once they issue it you can't change it.
提交者可以在发出拉取请求时更改它,但是一旦发出请求,您就无法更改它。
On the other hand, you can manually merge their branch and push, which I semi-regularly do for mistargetted pull requests.
另一方面,您可以手动合并他们的分支和推送,我半定期地为目标错误的拉取请求执行此操作。
You may find the hub
gemhelpful in working with the components of the pull request.
您可能会发现hub
gem有助于处理拉取请求的组件。
That gem wraps up the manual process, which is:
该 gem 结束了手动过程,即:
- Add a remotefor the fork to your local checkout.
- Fetch that remote.
git checkout ${target_branch} && git merge ${remote}/${branch}
git push origin ...
- 将 fork的遥控器添加到您的本地结帐。
- 拿那个遥控器。
git checkout ${target_branch} && git merge ${remote}/${branch}
git push origin ...
回答by Grzegorz Adam Hankiewicz
An alternative to using the hub gem mentioned by other answers is to use the command line to merge locally pull requests, which allows you to do:
使用其他答案提到的集线器 gem 的替代方法是使用命令行合并本地拉取请求,它允许您执行以下操作:
$ git fetch origin
$ git checkout *target_branch*
$ git merge pr/XXX
$ git push origin *target_branch*
The commands above only work directly if you first add the following line to your .git/config
file:
如果您首先将以下行添加到.git/config
文件中,则上述命令仅直接起作用:
fetch = +refs/pull/*/head:refs/remotes/symbolic_name_origin_or_upstream/pr/*
What that does is allow you to download ALLpull requests. Since that may not be desired for huge repos, GitHub modified the instructions to feature the git fetch origin pull/ID/head:BRANCHNAME
syntax, which avoids modification of the configuration file and only downloads that single pull request.
这样做是允许您下载所有拉取请求。由于大型 repos 可能不希望这样做,因此 GitHub 修改了说明以突出git fetch origin pull/ID/head:BRANCHNAME
语法,从而避免修改配置文件并且只下载单个拉取请求。
回答by Deckard
Although you cannot change the existing pull request as it is not yours you can easily create a new one if the related source repository still exists - yes, even if it is someone else's.
尽管您无法更改现有的拉取请求,因为它不是您的,但如果相关的源存储库仍然存在,您可以轻松创建一个新的拉取请求 - 是的,即使它是其他人的。
Go to the repository of the submitter then create a new pull request in his/her repository using the same commits but make sure you set the right target branch correctly.
转到提交者的存储库,然后使用相同的提交在他/她的存储库中创建一个新的拉取请求,但请确保正确设置了正确的目标分支。
Then go back to your own repository and accept the new pull request. Voila!
然后返回到您自己的存储库并接受新的拉取请求。瞧!
回答by Guillermo Mansilla
There is nothing wrong with Daniel Pittman's solution, however I would treat those merges as "no fast forward", that is, changing step number 3 for:
Daniel Pittman 的解决方案没有任何问题,但是我会将这些合并视为“没有快进”,也就是说,将第 3 步更改为:
git checkout ${target_branch} && git merge --no-ff ${remote}/${branch}
By using --no-ff
, the history will be easier to read. It will clearly say that $n
commits came from $branch
, and it will also make your life easier if you need to revert something done in that branch.
通过使用--no-ff
,历史将更容易阅读。它会清楚地说明$n
提交来自$branch
,并且如果您需要恢复在该分支中完成的某些事情,它也会使您的生活更轻松。
To also answer eoinoc's question and give an additional tip:
还要回答 eoinoc 的问题并提供额外提示:
After doing the merge, your git cli will prompt you to write a message, generally a generic message will show up saying something like
合并后,你的 git cli 会提示你写一条消息,一般会出现一条通用消息,说类似
Merge remote-tracking branch 'user/their-branch' into your-branch
将远程跟踪分支“用户/他们的分支”合并到您的分支中
Make sure to edit that message and include a reference to the pull request number. That is: (Assuming the pull request number is 123)
确保编辑该消息并包含对拉取请求编号的引用。即:(假设拉取请求编号为 123)
Merge remote-tracking branch 'user/their-branch' into your-branch
refs #123 solving whatever...
将远程跟踪分支“用户/他们的分支”合并到您的分支中
参考 #123 解决任何问题...
So next time you visit your github issues/pull-requests page and check that particular pull request, you will see your message with a link to commit where you did the merge.
因此,下次您访问您的 github 问题/拉取请求页面并检查该特定拉取请求时,您将看到您的消息,其中包含一个用于提交合并位置的链接。
Here is a screenshot of what I mean.
这是我的意思的截图。
回答by abbood
To do that go to your repository's home page, click on branches, and change the default branch from master into something else, in my case "dev".
为此,请转到存储库的主页,单击分支,然后将默认分支从 master 更改为其他分支,在我的示例中为“dev”。
After that, whenever someone creates a pull request the merge
button will automatically merge the request into "dev" rather than master.
之后,每当有人创建拉取请求时,merge
按钮都会自动将请求合并到“dev”而不是 master。