How to change the base branch of a pull request?

GithubPull Request

Github Problem Overview


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)?

Github Solutions


Solution 1 - Github

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 - Github

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 - Github

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 - Github

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 - Github

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.

Solution 6 - Github

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

Solution 7 - Github

#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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcos Vinícius da SilvaView Question on Stackoverflow
Solution 1 - GithubAmberView Answer on Stackoverflow
Solution 2 - GithubTomView Answer on Stackoverflow
Solution 3 - GithubMichael CliffordView Answer on Stackoverflow
Solution 4 - GithubSateeshView Answer on Stackoverflow
Solution 5 - GithubWilliam ZellerView Answer on Stackoverflow
Solution 6 - Githubjulian josephView Answer on Stackoverflow
Solution 7 - GithubhexaJerView Answer on Stackoverflow