Skip to content

Instantly share code, notes, and snippets.

@jjorissen52
Created June 29, 2022 21:25
Show Gist options
  • Save jjorissen52/69f15fd5f236b7890cd3bcd74b079802 to your computer and use it in GitHub Desktop.
Save jjorissen52/69f15fd5f236b7890cd3bcd74b079802 to your computer and use it in GitHub Desktop.
Clone all repos in all organizations you have access to
#!/usr/bin/env bash
# This script backs up all repositories in all organizations that the gh user has
# has access to.
cwd="$PWD"
orgs="$(gh api /user/memberships/orgs --jq '.[].organization.login')"
while read org; do
orgdir="./backups/$org"
mkdir -p "$orgdir"
repos="$(gh repo list "$org" --json sshUrl,name --jq ".[]")"
while read repo; do
name="$(echo "$repo" | jq -r '.name')"
sshUrl="$(echo "$repo" | jq -r '.sshUrl')"
repo_dest="$orgdir/$name"
if ! test -d "$orgdir/$name"; then
git clone "$sshUrl" "$repo_dest"
else
echo "$orgdir/$name already exists, skipping..."
fi
cd "$repo_dest"
# track and pull all branches from remote
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
# go back to starting directory
cd "$cwd"
done < <(echo "$repos")
done < <(echo "$orgs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment