Skip to content

Instantly share code, notes, and snippets.

@itsazzad
Forked from davegallant/gh-clone-org
Last active August 25, 2021 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsazzad/deb85a26732a08855cdde5f589c2ea02 to your computer and use it in GitHub Desktop.
Save itsazzad/deb85a26732a08855cdde5f589c2ea02 to your computer and use it in GitHub Desktop.
Git Organization: Clone Repo, Pull, Create Repo, Add Remote, Push
#!/usr/bin/env bash
# This script clones all repos in a GitHub org and pushes to the upstream
# It requires the GH CLI: https://cli.github.com
# It can be re-run to collect new repos and pull the latest changes
set -euo pipefail
USAGE="Usage: gh-clone-org <user|org> <target>"
[[ $# -eq 0 ]] && echo >&2 "missing arguments: ${USAGE}" && exit 1
org=$1
upstream=$2
limit=9999
repos="$(gh repo list "$org" -L $limit)"
repo_total="$(echo "$repos" | wc -l)"
repos_complete=0
echo
echo "$repos" | while read -r repo; do
repo_name="$(echo "$repo" | cut -f1)"
echo -ne "\r\e[0K[ $repos_complete / $repo_total ] Cloning $repo_name"
gh repo clone "$repo_name" "$repo_name" -- -q 2>/dev/null || (
cd "$repo_name"
git pull -q
gh repo create $upstream/$f --public --confirm
git remote add upstream git@github.com:$upstream/$repo_name.git
git push upstream
)
repos_complete=$((repos_complete + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment