79

I created a pull request on project on GitHub to a specific remote branch. After some time, the remote branch was deleted.

How can I change the pull request to point to another branch (specifically master)?

Ouroborus
  • 16,237
  • 4
  • 39
  • 62

7 Answers7

79

Updated: as Michael notes below, this is now possible:

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.

Click the Edit button by the title of the pull request to reveal the base branch selector.

An animated example of how to change a pull request's base branch.


Old answer

You can't. Just make a new pull request.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 10
    straight to the point, but not strictly true -- see [this](http://stackoverflow.com/a/11983910/211070) answer. – Tom Aug 16 '12 at 09:08
  • 7
    Relying on undocumented (and potentially unintentional) API functionality is a recipe for sadness down the line. – Amber Aug 18 '12 at 18:39
  • 15
    Nobody said anything about *relying* on undocumented API functionality. This question is asking for the resolution to a one-off problem, not a permanently supported ongoing solution. Regardless, to say that "You can't" is simply untrue. – Tom Sep 30 '12 at 20:12
  • 5
    Either way the undocumented API is broken now (see comments of the other answer), and that's pretty sad. – dequis Jun 16 '13 at 03:00
  • 1
    A quick googling dropped me into this issue with hundreds of +1s. https://github.com/isaacs/github/issues/18 – tejasbubane Feb 04 '15 at 07:04
  • 2
    As of 15.08.2016 you can. See Michael's answer. – maliayas Aug 16 '16 at 23:34
  • @Amber can you please explain me the terms `base` branch and `head` branch. I googled but could not found got any – Kasun Siyambalapitiya Dec 19 '16 at 12:09
  • Can you update your answer (to reflect Michael's) or delete this? – Duncan Jones Aug 15 '17 at 08:29
35

Although undocumented, you can do this using the GitHub REST API.

The usage of the API is explained in this answer, but basically you can issue a REST request like this one:

$ curl --user "tom" \
       --request PATCH \
       --data '{"issue": "15", "head": "tom:new-branch", "base": "master"}' \
       https://api.github.com/repos/fred/fabproj/pulls

This will change the pull request embodied by issue 15 on the fred/fabproj repo to use the new-branch branch on the tom/fabproj fork.

Edit: Note: according to comments, the above is only for attaching a new pull request to an existing issue.

Community
  • 1
  • 1
Tom
  • 4,742
  • 25
  • 32
  • Great, I'd initially submitted a pull request from my master branch, now I changed that to a feature branch – Koen. Aug 27 '12 at 14:03
  • 1
    Couldn’t get it to work. It complained that field `issue` had an incorrect value, maybe suggesting you can no longer change already instantiated pull requests. Shame. – mxcl Nov 13 '12 at 17:43
  • 1
    Are you sure you got your API call right? As far as I can tell, this should still work, and is (kinda) documented. See http://developer.github.com/v3/pulls/ (and search for "Create a pull request" then look at "Alternative Input") – Tom Nov 14 '12 at 17:04
  • Same issue here: { "errors": [ { "resource": "PullRequest", "value": 7, "code": "invalid", "field": "issue" } ], "message": "Validation Failed" } – Bradley Dec 13 '12 at 20:54
  • 14
    I've tried this out again today, and can confirm that it is no longer working. I'll keep exploring the REST APIs in the hope of discovering another way of doing this. – Tom Dec 13 '12 at 22:07
  • @Tom: just found this page because I needed to do the same. Did you find anything in the rest api? – Mizipzor Jan 04 '13 at 16:23
  • Sadly, not. I'll keep an eye out and update this answer if I find out more. – Tom Jan 06 '13 at 16:20
  • I tried the same trick and no luck. Api docs doesn't mention this type of updates anymore. – dangra Jan 08 '13 at 18:21
  • 1
    According to Developer docs the request type should bt PATCH. Other thing is head/base modification isn't supported at the moment. Ref: http://developer.github.com/v3/pulls/#update-a-pull-request – Shekhar Jan 21 '13 at 08:19
  • Did Github recently drop support for this? I tried it and couldn't get it to work. – Jian Apr 17 '13 at 21:30
  • 4
    Hey Jian, as Tom mentioned in the comments on Dec 13 '12, this is (sadly!) no longer working... which is a shame, as I could have made good use of it today, but alas! – pvandenberk Jun 03 '13 at 15:56
19

As of 08/15/2016 this is now possible natively via Github:

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.

2

I could change the target branch. It is true that we cannot edit the name of target branch in the PR. But the trick is to rename the branch to something else, and rename your target branch to that of present already in PR.

Example: My PR is having name like "dev-4.9". There is another branch which is named "qa-4.9". All I want is that "qa-4.9" should be the PR target branch. Steps:1 1) Re-name branch "dev-4.9" to something else "original-dev-4.9"

git checkout dev-4.9
git branch -w original-dev-4.9
git push origin original-dev-4.9

2) Re-name branch "qa-4.9" to "dev-4.9".

git checkout qa-4.9
git branch -w dev-4.9
git push origin dev-4.9 -f (force push to write entire branch to reflect dev-4.9)

3) Refresh PR url and see the commits in qa-4.9 reflected over there.

Sateesh
  • 31
  • 2
1

Instead of losing all the comments connected with a PR to a deleted branch:

  1. create the branch again locally with the same name and the same contents the branch you want to merge to has;
  2. push that branch to recreate the remote branch; and then
  3. reopen the PR to the branch.

For example, you have a PR to branch1, which is deleted. You now want to merge to master and retain comments on your existing PR:

  1. git checkout master
  2. git pull
  3. git checkout -b branch1
  4. git push
  5. reopen your PR to branch1
  6. when merged to branch1, merge to master.

This is a bit hacky, but far better than destroying lots of comments.

  • I didn't follow along to start off with so just to clarify for other readers (either >= dumb as me) this is for if you are in control of the repo your PR has been submitted to. I was looking for a way to update my PR that I've submitted to an open source project I don't control. – rtpHarry Apr 23 '16 at 14:28
0

Github supports this now. Edit button on the right end of the PR.

julian joseph
  • 330
  • 1
  • 4
  • 18
-2

In theory...

you're supposed to use github api.

example : edit pull request with curl

curl --user "your_github_username" \
     --request PATCH \
     --data '{"title":"newtitle","body":"newbody",...}' \
     https://api.github.com/repos/:owner/:repo/pulls/:number

you can find the detailled list of data in github developer doc

example : change name of my pull request

curl --user "jeremyclement" \
     --request PATCH \
     --data '{"title":"allows the control of files and folders permissions."}' \
     https://api.github.com/repos/Gregwar/Cache/pulls/9

but in practice...

it seems that the fields head/label and head/ref are not editable. For now, the only solution seems to be that of Amber

Community
  • 1
  • 1
hexaJer
  • 825
  • 7
  • 12