Is it possible to change the visibility of a private repo using the GitHub API?

Viewed 2156

I've been looking for a way to convert my private repositories to public ones, but since there are so many of them, I preferred to do it the easy way and resorted to the GitHub API, but this one, as far as I could find, only allows editing the visibility of repositories that are public. Looking through the most up-to-date documentation, I realized that the only endpoints available for access while authenticated are (Repositories, Endpoints available):

  • GET /user/repos
  • POST /user/repos
  • POST /user/projects

I also tried using the browser console and generating a log when I make a request to change visibility, but it doesn't seem to make any requests to the GitHub API.

2 Answers

You can sets a repo's visibility to public (or private) with this REST endpoint:

PATCH /repos/{owner}/{repo}

This does work for repos that are private (despite your comment). And if the repo already has the visibility being requested, the API will return an error message indicating that status.

visibility Parameter

Can be public or private. If your organization is associated with an enterprise account using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+, visibility can also be internal. The visibility parameter overrides the private parameter when you use both along with the nebula-preview preview header.

Examples

curl

curl \
  -X PATCH \
  -H "Accept: application/vnd.github.nebula-preview+json" \
  https://api.github.com/repos/octocat/hello-world \
  -d '{"visibility":"public"}'

gh

gh api -X PATCH /repos/octocat/hello-world -fvisibility=public --preview nebula

2022: I would use the GitHub CLI and its gh repo edit command:

cd /path/to/local/repo
gh repo edit --visibility=public 

2020: Yes, you can, but as mention in the repository API, this is still in beta:

You can set the visibility of a repository using the new visibility parameter in the Repositories API, and get a repository's visibility with a new response key.
For more information, see the blog post.

To access repository visibility during the preview period, you must provide a custom media type in the Accept header:

application/vnd.github.nebula-preview+json

Related