Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Find local git branches with a closed GitHub PR
# Outputs commands to delete local branches that have an
# associated merged (really closed) PR.
# In particular, this also works if the remote branch was
# before merging rebased remotely.
# BIG CAVEAT: This will report branches that have a closed,
# but not merged PR for deletion.
# Assumes two environment vars to exist:
for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
# Find closed PRs associated with the branch name.
# Note, that this ignores whether the PR was merged.
# TODO: Filter for only merged PRs.
json=$(curl --silent -X GET -H "User-Agent: amp-changelog-gulp-task" \
-H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/json" \
-H "Cache-Control: no-cache" \
"$GITHUB_USERNAME:$branch" 2>&1)
if [[ $json == {* ]];then
echo "ERROR $json"
exit 1
if [ "$json" != "[]" ];then
echo "git update-ref -d $branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.