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?

Totte Karlsson
  • 1,261
  • 1
  • 20
  • 55
  • 5
    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. – matt Jan 06 '21 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. – evolutionxbox Jan 06 '21 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. – Totte Karlsson Jan 06 '21 at 22:17
  • “Since nothing is ever really deleted in a repo” Believe that if you like! – matt Jan 06 '21 at 22:48
  • @matt Well, if my statement is wrong then tell us the truth, since you seem to have it. – Totte Karlsson Jan 06 '21 at 23:27

3 Answers3

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
jthill
  • 55,082
  • 5
  • 77
  • 137
  • 1
    @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. – jthill Jan 07 '21 at 04:34
0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I am pretty late here but giving answer as it may help others.

You can print total number of remote branches.

git branch -r | wc -l

git branch -r command is for remote branches and wc -l will print total number of branches.

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

happy coding and learning

Kushwaha
  • 850
  • 10
  • 13