Last active
November 9, 2022 16:29
-
-
Save ptdel/6cb7973a8f5324e8e0297a657d6f3e42 to your computer and use it in GitHub Desktop.
"Fork" all repositories from a public organization into a private organization
This file contains 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 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 [email protected]:$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 [email protected]:$TO_ORGANIZATION/$REPO.git | |
cd .. | |
rm -rf $REPO | |
echo "cloning $TO_ORGANIZATION/$REPO" | |
git clone [email protected]:$TO_ORGANIZATION/$REPO.git | |
cd $REPO | |
echo "setting upstream for $TO_ORGANIZATION/$REPO to $FROM_ORGANIZATION/$REPO" | |
git remote add upstream [email protected]:$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