Skip to content

Instantly share code, notes, and snippets.

@kristijorgji
Last active February 20, 2025 13:36
Show Gist options
  • Save kristijorgji/4873fad1e1e0b69ccbe6ebceb14a330c to your computer and use it in GitHub Desktop.
Save kristijorgji/4873fad1e1e0b69ccbe6ebceb14a330c to your computer and use it in GitHub Desktop.
A script allowing you to speed up the work and clone all repos of your organisation in one go
#!/bin/bash
#===============================================================================
# GitHub Organization Repository Cloner
# @kristijorgji
#===============================================================================
# This script clones all repositories of a specified GitHub organization into a
# designated folder. If a repository already exists, it pulls the latest changes.
#===============================================================================
# USAGE:
# ./clone_repos.sh <reposDir> <orgName>
#
# PARAMETERS:
# reposDir - Directory where repositories will be stored.
# orgName - GitHub organization name whose repositories will be cloned.
#
# REQUIREMENTS:
# - GitHub CLI (gh) installed and authenticated
# - jq installed for JSON parsing
# - git installed
#
# INSTALLATION:
# macOS:
# brew install gh jq git
#
# Linux (Debian/Ubuntu):
# sudo apt update && sudo apt install gh jq git -y
#
# Linux (Arch):
# sudo pacman -S github-cli jq git
#
# Linux (Fedora):
# sudo dnf install gh jq git
#
# AUTHENTICATION:
# Run `gh auth login` to authenticate with your GitHub account before using this script.
#===============================================================================
# Ensure correct usage
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <reposDir> <orgName>"
exit 1
fi
# Set parameters
reposDir="$1"
orgName="$2"
# Ensure the directory exists
mkdir -p "$reposDir"
# Fetch repository list from GitHub
gh repo list "$orgName" --limit 4000 | while read -r repo _; do
repoWithoutOrgName=$(echo "$repo" | sed 's|.*/||')
repoDir="$reposDir/$repoWithoutOrgName"
if [ -d "$repoDir" ]; then
echo "[update] - Pulling latest changes for $repoDir"
git -C "$repoDir" pull --rebase # Update existing repository
else
echo "[clone] - Cloning $repo into $repoDir"
gh repo clone "$repo" "$repoDir"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment