Reputation: 12155
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?
Upvotes: 18
Views: 18217
Reputation: 352
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.
Upvotes: 0
Reputation: 3816
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
Upvotes: 7
Reputation: 56462
Something like
git branch -r | wc -l
using a shell.
wc -l
counts the number of line on its input.
Upvotes: 61