Skip to content

Instantly share code, notes, and snippets.

@bluishoul
Last active April 9, 2026 10:13
Show Gist options
  • Select an option

  • Save bluishoul/87653d859fd3cb53f612aaaf63dfec94 to your computer and use it in GitHub Desktop.

Select an option

Save bluishoul/87653d859fd3cb53f612aaaf63dfec94 to your computer and use it in GitHub Desktop.
Cleanup unchanged forked repos using gh cli
#!/usr/bin/env zsh
# Array to store repos with no changes
unchanged_forks=()
# Use process substitution < <() so the array stays available after the loop
while read p_repo f_repo; do
echo "---------------------------------------------------"
echo "πŸ” Checking Fork: $f_repo"
# 1. Fetch branch names
p_branch=$(gh repo view "$p_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null)
f_branch=$(gh repo view "$f_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null)
if [[ -z "$p_branch" || -z "$f_branch" ]]; then
echo " ❌ SKIP: Could not retrieve branch names."
continue
fi
# 2. API Comparison
compare_url="repos/$f_repo/compare/${p_repo%/*}:${p_branch}...${f_repo%/*}:${f_branch}"
res=$(gh api "$compare_url" --jq '{status: .status, ahead: .ahead_by}' 2>/dev/null)
if [[ -z "$res" ]]; then
echo " ❌ ERROR: API 404 for $f_repo"
continue
fi
diff_status=$(echo "$res" | jq -r .status)
ahead_count=$(echo "$res" | jq -r .ahead)
# 3. Collect if no changes (ahead is 0)
if [[ "$ahead_count" == "0" ]]; then
echo " βœ… Found unchanged repo: $f_repo"
unchanged_forks+=("$f_repo")
else
echo " πŸ”Έ Modified: Ahead by $ahead_count"
fi
done < <(gh repo list --fork --limit 100 --json nameWithOwner,parent --jq '.[] | select(.parent != null) | "\(.parent.owner.login)/\(.parent.name) \(.nameWithOwner)"')
# --- Final Prompt ---
if [[ ${#unchanged_forks[@]} -eq 0 ]]; then
echo "\n✨ No unchanged forks found."
exit 0
fi
echo "\n⚠️ The following ${#unchanged_forks[@]} forks have NO personal changes:"
printf ' - %s\n' "${unchanged_forks[@]}"
# In Zsh, we read from the terminal specifically to avoid issues with the pipe
echo -n "\n❓ Delete these ${#unchanged_forks[@]} forks? (y/N): "
read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
for repo in "${unchanged_forks[@]}"; do
echo "πŸ—‘οΈ Deleting $repo..."
gh repo delete "$repo" --yes
done
echo "βœ… Cleanup complete."
else
echo "❌ Cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment