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

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. :)

Related

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

How to invite a user to a private github repo within an organisation using the command line

I am trying to add users to a private Github repo within an organisation. Starting from this post, I've simply changed the API endpoint to cope with organizations (as explained here), and I end up with the following command:
gh api orgs/MY_ORG/repos/MY_USER_NAME/MY_REPO/collaborators/COLLABORATOR_USER_NAME -f '{"permission":"maintain"}';
This command systematically returns a 404 error (note that I also get a 404 when I just try to check if a user has access to a repo, i.e. the GET version of the above command).
I also need to mention that this doesn't seem to be a trivial gh auth login issue since a command like gh repo create MY_ORG/MY_REPO works fine.
Here is also some technical details:
os: macosx 10.15.16
git: 2.24.3
gh: 1.1.0
I take the initiative to answer my own question here since after some investigations (thanks to mislav for his help) and trials and errors, I ve found the proper way to add collaborators to a GitHub repo within an organization with the CLI. I think it is worth posting it, hopefully this will help others.
Invite an outside collaborator to a repo within an organization
gh api -X PUT repos/:org/:repo/collaborators/:username -f permission=:perm
the -X PUT specifies that the request is a PUT and not a GET (default request). The repo's identifier is specified by :org/:repo (note that if the repo is not under an organization, the identifier will be :owner/:repo). The :perm argument indicates the type of access, the default value is push (see here)
So assume I want to provide admin access to jonsnow to the repo winterfell under the organization got, I will use the following command
gh api -X PUT repos/got/winterfell/collaborators/jonsnow -f permission=admin
Note that if you send an invite for the repo directly, the user will appear as an outside collaborator (not as an organization member)
Add a member to the organization and invite him to a repo
You just need to include the user as a member to the organisation beforehand with
gh api -X PUT /orgs/:org/memberships/:username -f role=:role
and then you can provide him access to a specific repo with the same command as above, i.e.
gh api -X PUT repos/:org/:repo/collaborators/:username -f permission=:perm
Note that the value for the various :role can be found here
You can set organization membership for a user
put /orgs/{org}/memberships/{username}
You can add a collaborator to a repo
put /repos/{owner}/{repo}/collaborators/{username}
But I don't think you can combine the two (add a collaborator to an org repo)
That is because that collaborator need to be a member of the organisation first (so receive and accept the invitation), before being added to a repository.

How to invite people to private GitHub repo through command line interface?

Usually one must click link "Invite teams or people" after accessing "https://github.com///settings/access" in a web browser.
But, I wish to do this through a command line interface, because I must invite
many persons. Is it possible?
You could use the GitHub API in order to add a collaborator
PUT /repos/:owner/:repo/collaborators/:username
See for instance here:
curl -H "Authorization: token YOUR_TOKEN" "https://api.github.com/repos/YOUR_USER_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME" -X PUT -d '{"permission":"admin"}'
With permission level being one of:
pull - can pull, but not push to or administer this repository.
push - can pull and push, but not administer this repository.
admin - can pull, push and administer this repository.
maintain - Recommended for project managers who need to manage the repository without access to sensitive or destructive actions.
triage - Recommended for contributors who need to proactively manage issues and pull requests without write access.
(default is "push")
Update Sept. 2020, considering GitHub CLI gh is now 1.0, it could be a good feature to add (a kind of gh repo invite)
In the meantime, you can use gh pi to make a similar API call, automatically authenticated, with -f to add POST fields.
gh api repos/YOUR_USER_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME" -f '{"permission":"admin"}'
An alternative using hub:
1- Check all users with permissions in your repo:
hub api --flat 'repos/YOUR_USER_OR_ORGANIZATION_NAME/YOUR_REPO/collaborators' | grep -E 'login|permissions'
2- Give permission to an user :
hub api 'repos/YOUR_USER_OR_ORGANIZATION_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME' -H X:PUT -H d:'{"permission":"admin"}'
You can use the github cli or call the github api directly through curl. In this example I add a member to a company repo using the github cli:
gh api "orgs/$target_repo/teams/$team/repos/$target_repo/$repo_new_name" -X PUT -f permission=admin
Also see the docs: https://docs.github.com/en/rest/reference/teams#add-or-update-team-repository-permissions
For your situation you can use this endpoint:
https://docs.github.com/en/rest/reference/repos#add-a-repository-collaborator

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

How to change the base branch of a pull request?

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