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
You can also verify if you are properly authenticated:
gh auth status
If you are not logged in, you can authenticate with:
gh auth login
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
For example:
gh repo edit username/my-repository \
--visibility private \
--accept-visibility-change-consequences
The important flag here is:
--accept-visibility-change-consequences
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
Example:
cd my-repository
gh repo edit --visibility private \
--accept-visibility-change-consequences
If everything goes well, GitHub CLI will show a message like this:
✓ Edited repository username/my-repository
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
The expected response should be:
{
"visibility": "PRIVATE"
}
You can also verify it from inside the local project folder:
gh repo view --json visibility
Expected result:
{
"visibility": "PRIVATE"
}
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
Or, if you are inside the local project folder:
gh repo view --web
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
Useful Command Summary
Change a repository to private:
gh repo edit OWNER/REPO \
--visibility private \
--accept-visibility-change-consequences
Change a repository to private from the local folder:
gh repo edit --visibility private \
--accept-visibility-change-consequences
Verify repository visibility:
gh repo view OWNER/REPO --json visibility
Open the repository on GitHub:
gh repo view OWNER/REPO --web
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)