Created
October 22, 2025 16:52
-
-
Save duboisf/981224fc6a2d448bc4515bdd612ecc8c to your computer and use it in GitHub Desktop.
List remote GitHub branches that are older than 1 year
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) | |
| ' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.