DEV Community

Cover image for How to Change a Public Repository to Private Using GitHub CLI
Cristian Jonhson Alvarez
Cristian Jonhson Alvarez

Posted on

How to Change a Public Repository to Private Using GitHub CLI

When working on personal, academic, or professional projects, we often start a repository as public to share progress, test configurations, or collaborate openly. However, at some point, it may become necessary to protect the code, especially if the project contains business logic, internal configurations, technical documentation, or an architecture that we do not want to expose yet.

In this article, we will see how to change a repository from public to private using GitHub CLI (gh), directly from the terminal.


Prerequisite

Before running the commands, you need to have GitHub CLI installed and configured.

You can check if gh is installed with:

gh --version
Enter fullscreen mode Exit fullscreen mode

You can also verify if you are properly authenticated:

gh auth status
Enter fullscreen mode Exit fullscreen mode

If you are not logged in, you can authenticate with:

gh auth login
Enter fullscreen mode Exit fullscreen mode

Important Security Note

Changing a repository from public to private does not remove any sensitive data that may have already been exposed. If you accidentally published secrets, API keys, tokens, or credentials, rotate them immediately.


Change a Public Repository to Private

The most direct way to change the visibility of a repository using GitHub CLI is:

gh repo edit OWNER/REPO \
  --visibility private \
  --accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

For example:

gh repo edit username/my-repository \
  --visibility private \
  --accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

The important flag here is:

--accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

GitHub requires this flag because changing the visibility of a repository can have consequences, such as affecting forks, stars, watchers, access permissions, or related integrations.


Change Visibility from the Local Repository Folder

If you are already inside the project folder and the local repository has its GitHub remote configured, you can run:

gh repo edit --visibility private \
--accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

Example:

cd my-repository
gh repo edit --visibility private \
--accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

If everything goes well, GitHub CLI will show a message like this:

✓ Edited repository username/my-repository
Enter fullscreen mode Exit fullscreen mode

This means the repository was updated successfully.


Verify That the Repository Is Private

After changing the visibility, it is a good practice to verify the repository status.

To check a specific repository:

gh repo view username/my-repository --json visibility
Enter fullscreen mode Exit fullscreen mode

The expected response should be:

{
  "visibility": "PRIVATE"
}
Enter fullscreen mode Exit fullscreen mode

You can also verify it from inside the local project folder:

gh repo view --json visibility
Enter fullscreen mode Exit fullscreen mode

Expected result:

{
  "visibility": "PRIVATE"
}
Enter fullscreen mode Exit fullscreen mode

This confirms that the repository is no longer public.


Open the Repository in the Browser

You can also open the repository directly from the terminal using:

gh repo view username/my-repository --web
Enter fullscreen mode Exit fullscreen mode

Or, if you are inside the local project folder:

gh repo view --web
Enter fullscreen mode Exit fullscreen mode

This will open the repository on GitHub in your browser, where you can visually review the configuration.


What Happens to Collaborators?

When a repository becomes private, only the following users will have access:

  • The repository owner
  • Manually added collaborators
  • Organization members with the corresponding permissions, if applicable

You can review the collaborators with:

gh api repos/username/my-repository/collaborators
Enter fullscreen mode Exit fullscreen mode

Useful Command Summary

Change a repository to private:

gh repo edit OWNER/REPO \
--visibility private \
--accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

Change a repository to private from the local folder:

gh repo edit --visibility private \
--accept-visibility-change-consequences
Enter fullscreen mode Exit fullscreen mode

Verify repository visibility:

gh repo view OWNER/REPO --json visibility
Enter fullscreen mode Exit fullscreen mode

Open the repository on GitHub:

gh repo view OWNER/REPO --web
Enter fullscreen mode Exit fullscreen mode

Conclusion

GitHub CLI makes repository management faster and more efficient directly from the terminal. Changing a repository from public to private is a simple task, but it is an important step when you need to protect source code, internal documentation, configuration files, or an unfinished project.

This workflow is especially useful for projects that are still under development, contain sensitive information, or are not ready to be publicly shared.

With a single command, you can update the repository visibility and verify the result without leaving the terminal.

Top comments (0)