Skip to content

Instantly share code, notes, and snippets.

@drmohundro
Created March 19, 2025 14:20
Show Gist options
  • Save drmohundro/83d637ab1180ea4d00496d9d83303e64 to your computer and use it in GitHub Desktop.
Save drmohundro/83d637ab1180ea4d00496d9d83303e64 to your computer and use it in GitHub Desktop.
Archive a GitHub Org Bash Script
#!/bin/bash
# Check if the organization or user is provided as a command-line argument
if [ -z "$1" ]; then
echo "Usage: $0 <organization_or_user>"
exit 1
fi
# Set the organization or user from the command-line argument
ORG_OR_USER="$1"
# Set the directory where you want to clone the repositories
CLONE_DIR="$HOME/dev/path-to-drop" # Changed to just /repos
# Create the clone directory if it doesn't exist
mkdir -p "$CLONE_DIR"
# Get the list of repositories using gh cli
repos=$(gh repo list "$ORG_OR_USER" --limit 1000 --json name --jq '.[].name')
# Loop through the repositories and clone them
while IFS= read -r repo; do
echo "Cloning $repo..."
gh repo clone "$ORG_OR_USER/$repo" "$CLONE_DIR/$repo"
if [ $? -ne 0 ]; then
echo "Failed to clone $repo."
continue # Skip to the next repo on failure
fi
# Change directory into the cloned repo
pushd "$CLONE_DIR/$repo" >/dev/null
# Run the git branch and pull commands
echo "Setting up branches and pulling for $repo..."
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
# Change back to the parent directory
popd >/dev/null
echo "Finished processing $repo."
done <<<"$repos"
echo "Finished cloning and setting up all repositories."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment