How to clone all repositories in a Github Organization

Kevin Simper
Feb 2, 2018 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store