List stale branches for a repository


I am trying to get the list of all the stale branches in my repository. But I can’t any see any API to get the stale branches against a repository.

Is there any API available for this ? or anyother way to do this ?

Your ideas are welcome


What do you mean by stale? Like, hasn’t been updated in a specified amount of time (how long)? What about branches that have commits that are on no other branches? You can get the branches and their most recent commit with the following v4 query: query { repository(owner: "python", name: "cpython") { refs(refPrefix: "refs/heads/", first: 99) { nodes { name target { ... on Commit { authoredDate abbreviatedOid } } } } } }

Thanks for your reply @pcm1289 

Yes you are right. A branch that hasn’t been updated in a specified amount of time (ex: 4 weeks) is a stale branch for my consideration.

I want to list the stale branches using github API. I think we can achieve this by using GraphQL, but I need to do this via Github API. Is there any way to do using Github REST API ? 

GraphQL sits on top of REST. Just make a POST with that query but make sure the set the right Accept and Authorization fields in your header. The bottom of this page has an example:

This gist might be helpful: A small script to find stale branches · GitHub

Since your local repository, located on the main branch (or master branch) you could use:

git branch -a --merged 

This list branches whose tips are reachable from the specified commit, by default HEAD

Then you can use bash in order to list specific information, like this:

for branch in `git branch -a --merged | grep -v main`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r