artm artm - 22 days ago 6x
Git Question

git list remote branches, order by number of commits

There is

git branch -r
to list all remote branches.

I wonder if there is away to list all remote branches, but order them by number of commits (eg., the branch with most commits is listed first).


I guess one usage is that it can filter out unused/obsolete branches (that have very commits) and tidy up the repo

That is what you see in the "branches" tab section of a GitHub project:

Example for git/git/branches:


What you want is not the number of commits, but, for a given branch acting as reference, the number of commit ahead and behind that branch.

git rev-list --left-right --count master...test-branch

That way you can see the one behind that could be safely removed.
Note that you can already list those merged branches with:

git branch --merged master

With Git 2.5, you can also list local branches compared to their remote tracking branches (not your case, but can be useful):

git fetch
git for-each-ref --format="%(push:track)" refs/heads