Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save duboisf/981224fc6a2d448bc4515bdd612ecc8c to your computer and use it in GitHub Desktop.

Select an option

Save duboisf/981224fc6a2d448bc4515bdd612ecc8c to your computer and use it in GitHub Desktop.
List remote GitHub branches that are older than 1 year
#!/bin/bash
gh api graphql --paginate -f query='
query($owner: String!, $repo: String!, $endCursor: String) {
repository(owner: $owner, name: $repo) {
refs(refPrefix: "refs/heads/", first: 100, after: $endCursor, orderBy: {field: TAG_COMMIT_DATE, direction: ASC}) {
edges {
node {
name
target {
... on Commit {
committedDate
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
' -F owner={owner} -F repo={repo} \
| jq --arg cutoff_date "$(date -d '1 year ago' --iso-8601=seconds)" -s \ '
map(
.data.repository.refs.edges
| .[].node
| {branch: .name, committedDate: .target.committedDate}
)
| flatten
| sort_by(.committedDate)
| .[]
| select(.committedDate < $cutoff_date)
'
@duboisf
Copy link
Author

duboisf commented Oct 22, 2025

You can use the output to cleanup some old branches.

Caution

Don't blindly run this, you might delete important branches in your GitHub repo.

... | jq .branch -r | xargs -n 1 git push --delete --dry-run origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment