How to change the base branch of a pull request? - github

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

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.
Old answer
You can't. Just make a new pull request.

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.

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.

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.

Instead of losing all the comments connected with a PR to a deleted branch:
create the branch again locally with the same name and the same contents the branch you want to merge to has;
push that branch to recreate the remote branch; and then
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:
git checkout master
git pull
git checkout -b branch1
git push
reopen your PR to branch1
when merged to branch1, merge to master.
This is a bit hacky, but far better than destroying lots of comments.

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

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

Related

How can i stop CircleCi from automatically deleting branches that are deleted from github?

I have a workflow on CircleCi that builds and deploys a branch to AWS when a pull request is open from that branch.
I have a second workflow that tears down the deployed environment when the pull request is merged.
NOW: When a branch is deleted from Github, it is also deleted from CircleCi. And most of the times, when the developers in my team merge their pull request, they delete the branch.
When this happens, CircleCi tries to teardown the previously deployed environment. The problem now is that since the branch was deleted from Github and thus automatically deleted from CircleCi, the request to start the teardown workflow fails with "branch not found" error.
The question now is, how can I make circleCi retain the branch that was deleted on Github so that this request will run? I can make the teardown job then delete the branch from CircleCi after the teardown is done.
The request to CircleCi to start the teardown workflow looks like this...
curl -X POST 'https://circleci.com/api/v2/project/github/<organization>/<project_name>/pipeline' \
-H 'Circle-Token: ${{env.CIRCLECI_TOKEN}}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d "{\"branch\":\"${{env.CIRCLECI_BRANCH}}\",\"parameters\":{\"pull_request_closed\":true}}"
I think the actual issue here is this:
\"branch\":\"${{env.CIRCLECI_BRANCH}}\"
This syntax to reference a CircleCI environment variable is incorrect.
Try this instead:
\"branch\": \"${CIRCLE_BRANCH}\"
 
Also, this statement:
When a branch is deleted from Github, it is also deleted from CircleCI
is not completely true.
Although the related branch might be deleted from the "Filter Branches" list, any build that ran on the now-deleted branch will still be present and visible in CircleCI.
You can access them either by:
Constructing a URL for the branch in question
https://app.circleci.com/pipelines/github/your_org_name/your_project_name?branch=your_deleted_branch_name
Scrolling through the "Pipelines" page to find a build that ran on that branch, then by clicking the branch name you'll see all builds that previously ran on the branch.

GitHub unicorn when creating PR: "This page is taking too long to load."

When creating a Pull Request on GitHub, the page refuses to load and I am instead shown a unicorn error message:
This page is taking too long to load.
Sorry about that. Please try refreshing and contact us if the problem persists.
This repo's default branch is set to master, which is well behind the development branch we merge into. This error is likely a result of there being too many commits or the resulting PR being "too big".
Is there another URL or method to create a PR without calculating the full diff of the default branch?
If you push a branch, GitHub will helpfully show a Compare & pull request button as a shortcut to creating a PR for that branch:
That button is useful in most circumstances, but does automatically compare against the default branch using this URL:
https://github.com/user/repo/compare/branch?expand=1
If the resulting diff is too big or complicated, the unicorn error message will be shown.
Instead, press the New pull request button. The subsequent page will allow you to select the PR's base and compare branches prior to generating the diff.
https://github.com/user/repo/compare
The diff into your development branch will be simpler, and you should no longer get the error.
I managed to solve by creating the PR via the GitHub API e.g.
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/OWNER/REPO/pulls \
-f title='Amazing new feature' \
-f body='Please pull these awesome changes in!' \
-f head='octocat:new-feature' \
-f base='master'
https://docs.github.com/en/rest/pulls/pulls#create-a-pull-request

github new branch creation and pull request - using REST API

I need to create a branch and then do a pull request using REST API. First i tired to create a new branch with the following curl command.
[root#localhost tmp]# curl -d '{ "ref": "refs/heads/new_branch", "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd" }' -u user-name -X POST https://api.github.com/repos/user-name/myrepo/git/refs
Enter host password for user 'user-name':
{
"message": "Object does not exist",
"documentation_url": "https://developer.github.com/v3/git/refs/#create-a-reference"
}
A better alternative (since February 2020) is to use the new GitHub cli, which replaces/offers an alternative to github/hub.
See "GitHub CLI is now in beta " and "Supercharge your command line experience":
It is cli.github.com.
If will create a fork for you:
Create a pull request
Create a branch, make several commits to fix the bug described in the issue, and use gh to create a pull request to share your contribution.
By using GitHub CLI to create pull requests, it also automatically creates a fork when you don’t already have one, and it pushes your branch and creates your pull request to get your change merged.
a good alternative is to use hub instead. https://github.com/github/hub
For example:
if you haven't cloned the repo yet:
hub checkout [PULLREQ-URL]
if you have a repo cloned already, and just want a new branch
hub checkout [BRANCH]
Using the following link to review the commands for creating a new branch (AKA: refs) and this link to review how to validate the credentials, I could create a new empty branch. If you use the command without the validation, you will get a confusing answer
"message": "Not Found"
I followed this step to create a new branch named:
Get the SHA using this command -> GET https://api.github.com/repos/{username}/{repository}/git/refs/heads and copy one SHA (In my case, all are the same)
Create a token using this link and copy it. This token will be used in the parameter to create the branch.
Create the new branch using this command -> POST https://api.github.com/repos/{username}/{repository}/git/refs, setting a basic authorization {username}:{token} in the header and in the body the follow content:
{
"ref": "refs/heads/{BranchName}",
"sha": "{SHA copied in the step 1}"
}
Finally, you will obtain the information of the new branch

Trigger a webhook every time something merge into master - GitHub Webhooks

I not sure exactly how this will work, but I am looking to run a bash script that updates master every time something merges into master through Github. Is there a GitHub webhook that could be triggered on a PR merge? Also, I am not sure how would I update master through this web hook? Or any other suggestions how this could be done? I don't know if I am heading in the right direction.
By updating master, I meant, pull everything from master. Run a bash script that makes few changes and push new changes back to master.
The GitHub webhook push event would be useful in this case. You would need an server/application to listen for the hook, e.g. a node.js/php server, or CI services.
In your server, check the webhook payload for ref === refs/head/master i.e.master branch, or other conditions you see fit. Then to modify the repo's master branch, run a local git command or call GitHub API with proper credentials.
P.S. Updating the master branch with this service would also trigger a webhook event, so maybe you want to check for pusher too in this case.

How to merge branches on github.com without doing pull request?

It looks like GitHub only allows merging of branches by making a pull request and then merging.
Is there a way to merge mobile into master in a single step without cloning locally?
I only see this button, which creates a pull request that needs to be merged in a second step:
Github does not provide such a mechanism - and by following best practices, it doesn't make sense for them to provide such a feature.
The steps are to Merge it on your machine, then Push:
git merge mobile
git push
Pull requests are really only for repositories you don't control, and/or some code review process.
Per comments on the question, if this isn't convenient for you, very likely it is a sign of going against best practices, hindering your ability to work correctly.
Github does not provide this functionality via the web UI at this point.
You can't do it on the website itself, but you can do it via the Branches API, without cloning locally:
curl \
--header "Authorization: token $TOKEN" \
--data '{"base":"master","head":"develop","commit_message":"YOOOO"}' \
https://api.github.com/repos/$USER/$REPO/merges
You can use GitHub Docs' GraphQL API Explorer to run a mergeBranch mutation:
mutation {
mergeBranch(
input: {
repositoryId: "MDEwOlJlcG9zaXRvcwNzI="
base: "master"
head: "cool_feature"
commitMessage: "Shipped cool_feature!"
}
) {
clientMutationId
mergeCommit {
oid
message
}
}
}
Note: You can look up your repository's id by using the repository query to search by owner and name.
{
repository(owner: "my-name", name: "my-repo") {
id
}
}
GitHub's desktop app lets you do this with the menu item Branch → Merge Into Current Branch.... Presumably it's just doing a git merge under the hood as in Eevee's answer, but it's nice and easy. :)