18

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?

1
  • 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 command for detail/control. Commented Dec 22, 2020 at 21:02

3 Answers 3

62

Something like

 git branch -r | wc -l 

using a shell.

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

Sign up to request clarification or add additional context in comments.

Comments

7

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 

1 Comment

Yes. Note though that there is something Powershell Core which you might able to use from other platforms I guess...
0

You can simply use the command

git branch -r | wc -l 

wc -l counts the number of branches. But what if you want to know the count of branches which are older and unmodified from long period of time. You can use

git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short) %(committerdate:relative)' | awk '$2 ~ /ago/ {print $1}' | wc -l 

This command uses 'awk' to filter branches with a commit date containing "ago" (indicating they are older than 30 days) and then pipes the result to wc -l to count the number of matching branches.

git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short) %(committerdate:relative)' | awk '$2 ~ /months/ || $2 ~ /year/ {print $1}' | wc -l 

This is to get the count of branch older than 60 days likely having a commit date string containing "months" or "year."

Feel free to adjust the conditions in the 'awk' command based on your specific needs or the format of the date strings in your Git repository.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.