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?
3 Answers
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"}'

- 1,262,500
- 529
- 4,410
- 5,250
-
Thanks very much! I could not come up with this option! – user15181 May 29 '20 at 05:38
-
@user15181 I have added the GItHub CLI `gh api` alternative, since the old CLI `hub` is now obsolete. – VonC Sep 19 '20 at 13:53
-
I am trying to do smth very similar but within an organization, I therefore adapted the endpoint but when I try to check if a user is listed as a collaborator with the command `gh api orgs/YOUR_ORG/repos/YOUR_USER_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME` it always return a 404, am I doing something wrong? – greg hor Oct 15 '20 at 07:38
-
@greghor Either the authentication [`gh auth login`](https://cli.github.com/manual/gh_auth_login) did not work or you do not have access to the repository. – VonC Oct 15 '20 at 11:19
-
@VonC thanks for the reply. Using the gh CLI for features that are genuinely supported (ie: features that do not rely on the API) like `gh repo create MY_ORGS/MY_REPO -t MY_TEAMS` works perfectly fine. Can it nevertheless be a problem with `gh auth login` ? – greg hor Oct 15 '20 at 11:53
-
1@greghor In that case, no. I would invite you to post a separate question (with your OS, git and gh version) in order to have a closer look at the issue. – VonC Oct 15 '20 at 12:07
-
@VonC, done. See the post [here](https://stackoverflow.com/q/64371517/5872695) – greg hor Oct 15 '20 at 12:26
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"}'

- 106
- 1
- 7
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

- 4,334
- 9
- 46
- 75