Skip to content

Instantly share code, notes, and snippets.

@ptdel
Last active November 9, 2022 16:29
Show Gist options
  • Save ptdel/6cb7973a8f5324e8e0297a657d6f3e42 to your computer and use it in GitHub Desktop.
Save ptdel/6cb7973a8f5324e8e0297a657d6f3e42 to your computer and use it in GitHub Desktop.
"Fork" all repositories from a public organization into a private organization
#!/usr/bin/env bash
## this script assumes that you have the GitHub CLI and jq installed.
## before you run this script you should run `gh auth status` to make sure that
## you are logged in, otherwise the script will not work.
while getopts ":t:f:n:" args; do
case $args in
t) TO_ORGANIZATION=$OPTARG;;
f) FROM_ORGANIZATION=$OPTARG;;
n) NUMBER=$OPTARG;;
\?) echo "invalid option"
exit;;
esac
done
list_repositories() {
gh repo list $FROM_ORGANIZATION -L $NUMBER --json name | jq -r '.[].name' | tr -d '"'
}
REPOSITORY_LIST=$(list_repositories)
for REPO in $REPOSITORY_LIST; do
echo "cloning $FROM_ORGANIZATION/$REPO"
git clone --bare git@github.com:$FROM_ORGANIZATION/$REPO.git
echo "creating $TO_ORGANIZATION/$REPO"
gh repo create $TO_ORGANIZATION/$REPO --internal
cd $REPO
echo "mirroring from $FROM_ORGANIZATION/$REPO to $TO_ORGANIZATION/$REPO"
git push --mirror git@github.com:$TO_ORGANIZATION/$REPO.git
cd ..
rm -rf $REPO
echo "cloning $TO_ORGANIZATION/$REPO"
git clone git@github.com:$TO_ORGANIZATION/$REPO.git
cd $REPO
echo "setting upstream for $TO_ORGANIZATION/$REPO to $FROM_ORGANIZATION/$REPO"
git remote add upstream git@github.com:$FROM_ORGANIZATION/$REPO.git
git remote set-url --push upstream DISABLE
git remote -v
cd ..
rm -rf $REPO
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment