How to change the base branch of a pull request?

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.