Skip to content

Instantly share code, notes, and snippets.

@jaumef
Last active March 25, 2026 13:53
Show Gist options
  • Select an option

  • Save jaumef/0b593d0c67d7dbad2dee2db8fc762d82 to your computer and use it in GitHub Desktop.

Select an option

Save jaumef/0b593d0c67d7dbad2dee2db8fc762d82 to your computer and use it in GitHub Desktop.
Clean local branches with PR status via github client
#!/bin/bash
summary_merged=''
summary_closed=''
summary_open=''
summary_draft=''
summary_kept=''
for branch in $(git branch | grep -v master | grep -v '*')
do
branch_pr_status=$(gh pr view $branch | grep 'state:' | awk '{print $2}')
if [[ $branch_pr_status ]]
then
echo "$branch is $branch_pr_status"
if [[ "$branch_pr_status" == "MERGED" ]]
then
git branch -D $branch
summary_merged="$summary_merged$branch"$'\n'
elif [[ "$branch_pr_status" == "CLOSED" ]]
then
git branch -D $branch
summary_closed="$summary_closed$branch"$'\n'
elif [[ "$branch_pr_status" == "OPEN" ]]
then
summary_open="$summary_open$branch"$'\n'
elif [[ "$branch_pr_status" == "DRAFT" ]]
then
summary_draft="$summary_draft$branch"$'\n'
else
echo "UNEXPECTED STATE:'$branch_pr_status'"
summary_kept="$summary_kept$branch"$'\n'
fi
else
summary_kept="$summary_kept$branch"$'\n'
fi
done
echo "----MERGED----"
echo "$summary_merged"
echo "----CLOSED----"
echo "$summary_closed"
echo "----OPEN----"
echo "$summary_open"
echo "----DRAFT----"
echo "$summary_draft"
echo "----NO PR----"
echo "$summary_kept"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment