184

I have a pull request that is requesting a merge into master from my branch, but the owner wants me to change the request to merge into a different branch from my branch.

Is this possible? What are the possible solutions?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Moriarty
  • 2,067
  • 2
  • 11
  • 13

5 Answers5

237

Update August 2016: Change the base branch of a Pull Request finally allows for changing that branch.
(And this closes issue 18, which was 3 years old and had 1500+ comments)

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.

enter image description here

As noted in the comments byspazm:

It is (and was) working.

Changing target branch is not enabled while the PR is closed.
Solution was to "reopen" the PR (meaning by using the option in the UI) and then change the target branch


Original answer:

Since a PR cannot be edited in term of branch (see below), the easiest way would be to:

  • create a local new branch on top of your current branch
  • push that new branch
  • make a new PR with the right destination.
  • close the previous PR
  • reference the "old" pull request from the new one; eg. Supersedes #123 (as commented below by Rivera)

(original answer, valid only when creating the PR)

You could try and chose another base branch, as in "Changing the branch range and destination repository" (Clicking on the Edit button at the top of a PR page)

https://github-images.s3.amazonaws.com/help/pull_requests/pull-request-review-edit-branch.png

The easiest way of thinking about the branch range is this:

  • the base branch is where you think changes should be applied,
  • the head branch is what you would like to be applied.

Changing the base repository changes who is notified of the pull request.
Everyone that can push to the base repository will receive an email notification and see the new pull request in their dashboard the next time they sign in.

If you select as a base branch the one that the original maintainer (the owner) wants, your PR should merge from your branch (unchanged) to the new base branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Moriarty I don't have a PR in progress, but as the creator of the PR, you don't see an Edit button once your PR is sent? – VonC Jun 11 '14 at 09:30
  • @Moriarty if not, I have added a workaround in my (now edited) answer. – VonC Jun 11 '14 at 09:33
  • 3
    I do, but that only lets me modify the PR name. I'm assuming you can't directly change PR merge into branch on submit. I'm wondering if there is an alternative (way to merge a PR into another branch regardless). – Moriarty Jun 11 '14 at 09:33
  • You can create a new branch (with the same history as your current one), and make a new PR out of it: see my edited answer. – VonC Jun 11 '14 at 09:34
  • Noted, and that is similar to what I did (close the PR and made a new one). However, I'm wondering if there is a cleaner way. Can the owner merge the previous PR into a different branch to close the PR? – Moriarty Jun 11 '14 at 09:36
  • @Moriarty he cannot merge in a different branch from the GitHub web interface. A new PR is easier to manage. – VonC Jun 11 '14 at 09:36
  • 2
    There's a feature request open to be able to change the target branch of a PR: https://github.com/isaacs/github/issues/18 – Jason Antman Dec 07 '14 at 16:06
  • 1
    It is also a good idea to reference the "old" pull request from the new one (eg. Supersedes #123). – Rivera Sep 28 '15 at 14:11
  • @Rivera Good point. I have included your comment in the answer for more visibility – VonC Sep 28 '15 at 17:39
  • I am in this situation, where I have created a PR for branch X and it was switched on GitHub to branch Y. My local git is still on branch X. What do I need to sync them? I want to add some new commits to my PR. – Adam Aug 13 '19 at 10:58
  • @Adam Switch to Y, and cherry-pick the commits from X you need on your new Y PR branch. – VonC Aug 13 '19 at 11:51
  • Is it possible to change the source branch discarding all commits? – Shimmy Weitzhandler Apr 03 '20 at 03:34
  • 1
    @Shimmy I don't think so, not from the web GUI. – VonC Apr 03 '20 at 04:11
  • okay.. but what about the scenario where it's an attempt to retarget to upstream repo branch as the base? .... given that the initial PR was created for a forked repo (remote) branch that specified the base branch in that same/forked repo. (... which I would think is a way more common and routine scenario then people creating branches on the upstream repo's and having to retarget base. ) should that be a different question and issue written up, or is that actually something that's covered by this? (i.e. original answer) – Brett Caswell Jun 19 '21 at 04:45
  • @BrettCaswell Not sure I fully get the issue? Is it because the upstream repo branch is not in the fork? – VonC Jun 19 '21 at 12:52
  • yep, though I'm not sure if/why it says 'base fork' (in your create PR gif above). it specifies as 'base repository' when I attempt to create a PR. However, for editing a PR, I see no way of 'comparing across forks/repositories', (for example, the repository you've forked off of).. which is also noted in the gif in this answer. – Brett Caswell Jun 19 '21 at 13:12
  • @BrettCaswell OK. Than I would import that upstream branch into my current fork: that would help for comparing branches, from within the same repository. – VonC Jun 19 '21 at 16:35
  • Anyone know when this stopped working? – spazm Oct 06 '21 at 02:03
  • @spazm What has stopped working? – VonC Oct 06 '21 at 05:25
  • Update: It is (and was) working. Changing target branch is not enabled while the *PR is closed*. Solution was to reopen the PR and then change the target branch. – spazm Oct 14 '21 at 08:38
  • @spazm Thank you for this feedback. I have included your comment in the answer for more visibility. – VonC Oct 14 '21 at 11:00
  • @VonC - for clarity: by "reopen" I mean to use the option in the ui, rather than creating a new branch + PR. Or requesting a maintainer to do so if you don't have the privileges – spazm Oct 28 '21 at 19:57
  • 1
    @spazm Good point. I have edited the answer again, but don't hesitate to edit it yourself, and further clarify your comment that way. – VonC Oct 28 '21 at 20:40
6

If your branch is (still) available remotely, you do not have to create a new branch locally.

  • Select your branch (which bears the changes you are submitting) either directly from the drop-down menu or under "# branches" (https://github.com/account/project/branches).

  • Click "New pull request". This lets you choose a new base (target) branch.

Here is a screenshot of Github's new interface: enter image description here

I can submit a PR of branch UX against any fork/branch---it doesn't matter whether I previously submitted a PR for branch UX.

mkcor
  • 489
  • 4
  • 6
  • 1
    The green "New Pull request" button is moved to the PR list web page, at the top right corner. – Golden Thumb Dec 30 '15 at 13:54
  • I'm not sure what you mean by "is moved" but, yes, you can find the green button "New pull request" in both places. – mkcor Jan 10 '16 at 01:24
6

You can now: https://github.com/blog/2224-change-the-base-branch-of-a-pull-request

"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."

MathiasTCK
  • 316
  • 3
  • 8
0

You have to create a new pull request as mentioned in the GibHub documentationForm

  1. First select New Pull request
  2. in the compare page, left side select the target branch you want to push and right side select the branch you already pushed.
  3. Update the details for your new PR.
  4. Create the PR
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
-4

It's pretty simple. Just click the PR for which you want to change its base branch. Find "Close pull request" button near the bottom of the web page. Now you can recreate a new PR normally - make sure you select the correct base branch this time.

Golden Thumb
  • 2,531
  • 21
  • 20
  • 1
    I don't see why this is downvoted. This worked totally fine for me. – Wilt Mar 16 '16 at 09:47
  • 4
    I didn't down vote. But I see that this answer just repeat existing older answers (and it does so with less quality). – Lars Blumberg Mar 25 '16 at 19:36
  • 1
    @LarsBlumberg If you read carefully you should notice that it's not a repetition, even though there might be some overlaps. To me, if (n -1) steps, instead of n steps, can solve the problem, it's worth sharing :-) – Golden Thumb Mar 26 '16 at 01:06
  • 2
    If (n-1) steps leave out helpful steps such as closing the old PR and referencing in the new one, I don't see value in repeating a subset of steps from an already existing answer. Especially when the list of less steps is harder to read due to lack of proper formatting. – Lars Blumberg Mar 26 '16 at 08:48