Home > front end >  How do I set all GitHub repositories private with one command?
How do I set all GitHub repositories private with one command?

Time:08-05

Is it possible to set all repositories from public to private with one command?

CodePudding user response:

To list all of your public repositories:

 curl --request GET https://api.github.com/users/testUser/repos

To set a repository of user to private :

curl -u testUser:TOKEN --data "{\"private\": \"true\"}" --request PATCH https://api.github.com/repos/testUser/testRepo

To set all repositories owned by user to private :

curl --request GET https://api.github.com/users/testUser/repos | jq --raw-output '.[] .name' |  xargs -I % curl -u testUser:TOKEN --data "{\"private\": \"true\"}" --request PATCH https://api.github.com/repos/abc/%

Info:

  • Replace testUser everywhere with your username on GitHub.
  • Replace TOKEN with your personal access token for command line. To generate a Token follow link
  • jq can be installed from this link
  • curl util can be downloaded from this link

CodePudding user response:

You could use the GitHub CLI:

  • Fetch all repos currently not private
  • Update each repo to private

Something like this:

gh repo list OWNER --json nameWithOwner,isPrivate \
    --jq 'map(select(.isPrivate | not)) | .[].nameWithOwner'

where OWNER is the username or the organization whose repositories you want to update. If there are more than 30 repositories, you have to fetch them with an additional --limit option, for example gh repo list OWNER --limit 300.

The output is a list formatted like

OWNER/repo1
OWNER/repo2

each of which we can updated like this:

gh repo edit OWNER/repo1 --visibility private

In a single command:

gh repo list OWNER --json nameWithOwner,isPrivate \
    --jq 'map(select(.isPrivate | not)) | .[].nameWithOwner' \
    | while IFS= read -r repo; do
        gh repo edit "$repo" --visibility private
    done
  • Related