Skip to content

Instantly share code, notes, and snippets.

@Jerdak
Created September 23, 2023 03:00
Show Gist options
  • Save Jerdak/e35e2bd1f841d347871ca43759283fc8 to your computer and use it in GitHub Desktop.
Save Jerdak/e35e2bd1f841d347871ca43759283fc8 to your computer and use it in GitHub Desktop.
Bulk git backup and removal using Github CLI
# Back up all repositories starting with a certain set of characters.
#
# Replace `<GithubAccountName>/...` with the start of the repo(s) you want to remove. e.g. Jerdak/foo_
gh repo list --json nameWithOwner --jq '.[] | select(.nameWithOwner | startswith("<GithubAccountName>/...")) | .nameWithOwner' |
while read -r repo; do
owner=$(echo "$repo" | cut -d '/' -f 1)
repo_name=$(echo "$repo" | cut -d '/' -f 2)
gh repo clone "$owner/$repo_name"
cd "$repo_name"
tar czf "../${repo_name}.tar.gz" .
cd ..
rm -rf "$repo_name"
done
# Delete all repositories starting with a certain set of characters
#
# Replace `<GithubAccountName>/...` with the start of the repo(s) you want to remove. e.g. Jerdak/foo_
gh repo list --json nameWithOwner --jq '.[] | select(.nameWithOwner | startswith("Jerdak/difz")) | .nameWithOwner' |
while read -r repo; do
owner=$(echo "$repo" | cut -d '/' -f 1)
repo_name=$(echo "$repo" | cut -d '/' -f 2)
echo "Deleting: $owner/$repo_name"
# UNCOMMENT ME TO ACTUALLY DELETE (will require `gh auth refresh -s delete_repo` to have been run at least once)
#gh repo delete "$owner/$repo_name" --yes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment