2

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).

artm
  • 17,291
  • 6
  • 38
  • 54
  • I don't know how to sort `git branch` as you'd like, but as a step in the right direction [this is how you can determine how many commits are on each branch](http://stackoverflow.com/questions/10913892/number-of-commits-on-branch-in-git) – Cory Kramer Sep 30 '16 at 12:00
  • 1
    There is nothing built in. You could build it easily enough, using `git for-each-ref`, `git rev-list --count`, and the `sort` program. The result would not be very useful though as the count of commits on a branch is hardly ever worthwhile: a new branch, just created right now from `master`, has every commit that `master` has. (You probably want to redefine "how many commits" as something like "how many *unique* commits", perhaps.) – torek Sep 30 '16 at 12:00
  • @torek "the count of commits on a branch is hardly ever worthwhile" I guess one usage is that it can filter out unused/obsolete branches (that have very commits) and tidy up the repo. That's a reason I asked. I'll try out your suggestions – artm Sep 30 '16 at 12:07
  • @artm: See VonC's answer for one way to count commits across a symmetric difference between two branches. The more common expression is `git rev-list --count X..Y` (two dots rather than three): `X..Y` means `Y ^X` which means "the set of all commits on `Y` minus the set of all commits on `X`" (i.e., set subtraction), and `--count` then counts the number of elements in that set. Here `Y` is the branch you want to count, but you must also name some other branch `X`, otherwise in a typical repository with tens of thousands of commits, you will find tens of thousands of commits on every branch. – torek Sep 30 '16 at 20:55

1 Answers1

2

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:

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Interesting, I never knew `--count` could be combined with `--left-right` to count the left and right sides of the symmetric difference! – torek Sep 30 '16 at 20:50
  • @torek I still feel, after seeing the kind of shorthand like `%(push:track)`, there should be a shorthand for that kind of behind/ahead display between two branches. – VonC Sep 30 '16 at 21:02
  • Well, there is (sort of), but only for `@{u}`, and it's `git status` :-) The @{upstream} syntax is not very pretty, but the only alternative I know of is Mercurial's generalized expression parser, which is heavy-weight. It has great power, but with shell vagaries requiring quoting, it's pretty klunky syntactically too. – torek Sep 30 '16 at 21:12
  • Thanks @VonC . The ahead/behind info you provided is great. I also wanted to know which branches have only one or two commits - that way I can indentify those branches that had been created but not used and delete them. Some old branches can be a lot behind master but their commits may still be useful to keep. – artm Oct 01 '16 at 07:03
  • @artm those same command will do that: a branch has one or two commits compared to another branch. – VonC Oct 01 '16 at 07:27