14

How can I get the total number remote branches in Git?

To get all the remotes branches, I do this statement below, but I cannot get the count/total of these branches. I tried --count, but it didn't work:

  git branch -r

How would I get just the count of these?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
РАВИ
  • 11,467
  • 6
  • 31
  • 40
  • 2
    From Powershell v3 you can use the Count method on the result array, e.g. `(git branch -r).Count`. In Powershell v7.1 you also have the [Mesure-Object](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7.1) command for detail/control. – Aaron Dec 22 '20 at 21:02

2 Answers2

49

Something like

 git branch -r | wc -l

using a shell.

wc -l counts the number of line on its input.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
blue112
  • 52,634
  • 3
  • 45
  • 54
5

For PowerShell users (after all PowerShell is now also available cross-platform, not only on Windows), these are two commands giving you the remote branch count:

# Works in PowerShell before v3
(git branch -r | measure-object -line).Lines

# Works in PowerShell v3 or later
(git branch -r).Count
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22