DEV Community

Benjamin Black
Benjamin Black

Posted on • Updated on

Get a list of all of your GitHub repositories

My posts are usually notes and reference materials for myself, which I publish here with the hope that others might find them useful.

Requires the jq command-line JSON processor utility, here called using npx.

Replace <username> with your GitHub username and be prepared to enter your GitHub password on the command line.

If you have more than 100 repositories, you'll need to run this command several times, editing the page=? parameter each time.

curl --silent --user "<username>" "https://api.github.com/user/repos?page=1&per_page=100" | npx jq '.[].ssh_url' | while read repo
do
    repo="${repo%\"}"
    repo="${repo#\"}"
    echo "$repo"
done
Enter fullscreen mode Exit fullscreen mode

This nonsense repo="${repo%\"}" strips the leading and trailing double-quotes " from the line.

If you want the git:// or https:// schemes instead of SSH, then in the jq query, replace ssh_url with git_url or clone_url.

To save the output to a text file, append a redirect to the end, as in:

   ...
   echo "$repo"
done > output.txt
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
nelsonaloysio profile image
Nelson • Edited

Had to do some simple edits for terminal execution (and add /users/ to the URL):

[[ $1 = "" ]] &&
echo Error: missing username argument. ||
curl --silent "https://api.github.com/users/$1/repos?page=1&per_page=100" |
npx jq '.[].ssh_url' |
while read repo
do
    repo="${repo%\"}"
    repo="${repo#\"}"
    echo "$repo"
done | sort
Enter fullscreen mode Exit fullscreen mode

Also omitted the username password request, but that's for generally faster usage. :)

Thanks for sharing!

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

I love the git API. Don't use it much, but it can be very useful.

Also could just do echo ${repo//\"/}, assuming there are no quotes on the URI/