Created
June 29, 2022 21:25
-
-
Save jjorissen52/69f15fd5f236b7890cd3bcd74b079802 to your computer and use it in GitHub Desktop.
Clone all repos in all organizations you have access to
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
#!/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