How to clone all repositories in a Github Organization

I wanted to download and have all the repositories that we have at connectedcars on a single place on my computer so that I could easily go into each project. It would take me quite a way to go a find the url one by one and copy-and-paste to the git clone command. I knew you could get the repos from the Github API and I had already a Github Personal Access token in my bash profile.

So i could first get all the repositories like this

curl -s https://$GITHUB_AT:@api.github.com/orgs/YOURORGANIZATION/repos?per_page=200

I needed to put -s in front so that the output of was as not carried on to the next command.

You would get a huge object like something like this for each:

https://gist.github.com/kevinsimper/0615a032c2eca88102d1b2a7abe667e6

Then I can use the really cool JSON parser called jq and read the ssh-url from each object inside the array:

jq .[].ssh_url

Really simple and effective!

Then we can call the tool xargs which is made to call a command for each item it gets from the previous command.

xargs -n 1 git clone

We have to tell xargs only to take 1 at a time because git can’t clone multiple repositories at a time. The url will then get be appended to the end of git clone and all put together it look like this:

$ curl -s https://$GITHUB_AT:@api.github.com/orgs/connectedcars/repos?per_page=200 | jq .[].ssh_url | xargs -n 1 git clone

And when you run it, it will fairly quickly clone all your repositories.