How to Clone Every Repository in Your GitHub Account
The quick and dirty KISS way
On a fresh macOS installation, I wanted to pull down all my GitHub repositories (i) to work on my code locally and (ii) because redundant backups are never a bad thing.
I started digging around in the GitHub API, but getting set up with tokens and authentication (for private repositories) seemed like overkill for a one-off task like this. After half an hour of Stack Overflow and Postman, I gave up on the API method.
I didn’t want to have to clone each of the 50-odd repositories individually — at least not manually — because programmers are lazy.
Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don’t have to answer so many questions about it.
Keep It Simple, Stupid
Then, it dawned on me. If I could just get the exact names of all my repositories, I could write a shell “script” to iteratively
git clone them. So that’s what I did, and what you can do. It’s a quick read, so I suggest reading it through before following step-by-step.
Caution: GIFs ahead.
Preconditions and Assumptions
- You can login to your GitHub/BitBucket/GitLab account in your web browser.
- Any hotkeys mentioned are for a Mac.
- I’m using VS Code.
- Login to GitHub and copy the names of all your repositories to the clipboard. If you have more than a few repositories, there’s a
Load more...button at the bottom of the list:
2. Paste the names in your favourite multi-cursor-capable text editor:
3. Select all, and split your selection into multiple cursors — one per line:
4. Prepend each line with
git clone https://YOUR_USERNAME@github.com/YOUR_USERNAME/. If you’re using an SSH key (and you’ll know if you are, because you set it up) then prepend each line with
git clone firstname.lastname@example.org:YOUR_USERNAME/ instead.
NOTICE: in the GIF above, the
automagica/automagicarepository belongs to an organization. Prepending my username to an organization’s repository will create an invalid URL — so this command would fail. For organizations’ repositories, prepend with
5. Similarly, append each line with
6. Save your file as
cloner.sh within the directory you’ve chosen to store your repositories — for me, that’s
~/Developer — and navigate to that directory in your terminal.
7. Now it’s time to execute. Run
sh ./cloner.sh to run all the commands in the file. Keep in mind that if you’re *not* using an SSH key, you’ll have to enter your password for every private repository you’re cloning…
…and there are ways around it, but ultimately, if you have more than a few private repo’s, it’s worth your time to set up the SSH key, which is great for general use with GitHub, BitBucket, or GitLab anyway.
As always — if you have any questions or issues, hit me up on Twitter. ✌️