0

I'm working on a large git repository and are trying to get some metrics. One such metric is the number of branches ever created and in use.

From this post: Get total remote branches in git, one can get the number of current branches.

But how to get the number of branches ever created in a repository, included deleted ones?

5
  • 6
    You can’t. Branches are not things. They are just labels and they can be quite ephemeral, as you yourself have admitted. Deleted is deleted. Plus one branch can merge multiple times. It’s a meaningless idea for a metric. Commented Jan 6, 2021 at 21:42
  • You might be able to track it on your own computer by watching the .git/refs/heads directory? But not all branches ever, no. Commented Jan 6, 2021 at 21:47
  • Thanks for the comments @matt and evolutionxbox. I disagree its meaningless for a metric as it would indicate the usage of branches overall. If you have a repository with 5000 commits and 100 contributors on one branch, it indicates a problem, versus 5000 commits and say 1000 branches. Those two cases indicate two very different usages of the repo. One probably indicating some quite bad practices.. Since nothing is ever really deleted in a repo, I believe there may a way to traverse the repo in time to get the total. Merging multiple times is expected. Commented Jan 6, 2021 at 22:17
  • “Since nothing is ever really deleted in a repo” Believe that if you like! Commented Jan 6, 2021 at 22:48
  • @matt Well, if my statement is wrong then tell us the truth, since you seem to have it. Commented Jan 6, 2021 at 23:27

3 Answers 3

3

As a possible substitute metric, you could find out how many commits that are ~extra~ children of a commit have single parents, those were new branches off an existing root. Count those and the roots, you've got total actual branches in history. This won't tell you about any branches that never wound up contributing any commits.

( git rev-list --all --children; echo; git rev-list --all --parents --no-merges ) \ | awk ' !doneloading && NF>2 { i=2; while(++i<=NF) branchchild[$i]=1 } /^$/ { doneloading=1 } doneloading && (NF==1 || $1 in branchchild) { print $1 } ' | wc 
Sign up to request clarification or add additional context in comments.

1 Comment

@VonC thanks. It's got its weaknesses, Git doesn't care about this so which descendants are ~extra~ gets a bit arbitrary, and depends on your choice of traversal order. But the number's a navel-gazer anyway.
2

You can print the total number of remote branches using:

git branch -r | wc -l 

The git branch -r command is for listing the remote branches and wc -l will print the total number of lines of the output from the previous command, which in this case would be the total number of branches.

Note: It will give you existing branches, if you permanently deleted the branches then you cannot get them by executing the command above.

Comments

1

Since nothing is ever really deleted in a repo

Not quite. The git reflog (for 90 days by default) or git fsck commands can help you list old commits, but you would still have to remember the branch name in order to restore it (as in here).

And that supposes a direct access to the common repository where those 100 contributors are pushing to.

If the remote repository is managed by a repository hosting service, like for instance GitHub, you would use the Events API in order to find back the trace of deleted branches.

In both instances though, once a branch is deleted, it is not easy to find it back, and that would be even trickier for all past deleted branch.

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.