Created
February 8, 2026 11:32
-
-
Save maxgfr/3046c944a3a25a05f8320a6257c4c7cd to your computer and use it in GitHub Desktop.
Bash script to clone all public and private repositories from a GitHub organization using the GitHub REST API and a Personal Access Token. Handles pagination, skips already cloned repositories, and works out-of-the-box for full org backups or migrations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ===== CONFIG ===== | |
| ORG_NAME="org" | |
| GITHUB_TOKEN="abcdefgh" | |
| CLONE_DIR="." | |
| PER_PAGE=100 | |
| # ================== | |
| mkdir -p "$CLONE_DIR" | |
| cd "$CLONE_DIR" || exit 1 | |
| PAGE=1 | |
| while true; do | |
| RESPONSE=$(curl -s \ | |
| -H "Authorization: token $GITHUB_TOKEN" \ | |
| "https://api.github.com/orgs/$ORG_NAME/repos?per_page=$PER_PAGE&page=$PAGE") | |
| REPO_COUNT=$(echo "$RESPONSE" | jq length) | |
| if [ "$REPO_COUNT" -eq 0 ]; then | |
| break | |
| fi | |
| echo "$RESPONSE" | jq -r '.[].clone_url' | while read -r REPO_URL; do | |
| # Inject the token into the HTTPS URL | |
| AUTH_REPO_URL=$(echo "$REPO_URL" | sed "s|https://|https://$GITHUB_TOKEN@|") | |
| REPO_NAME=$(basename "$REPO_URL" .git) | |
| if [ -d "$REPO_NAME" ]; then | |
| echo "✔ $REPO_NAME already cloned, skipping" | |
| else | |
| echo "⬇ Cloning $REPO_NAME" | |
| git clone "$AUTH_REPO_URL" | |
| fi | |
| done | |
| PAGE=$((PAGE + 1)) | |
| done | |
| echo "✅ All repos have been downloaded." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment