0

Is there a way to easily get the count of the number of branches created from a specific (non-main) branch?

Note I'm not looking for the total number of branches, but ideally a table showing branch name and the number of branches created from that branch.

pra
  • 8,479
  • 2
  • 18
  • 15
  • 1
    Define "created from that branch"? What if you create one branch from A, name it B and check it out, make a commit on B, then create another branch C from B. Is C "created from A" or not? What if you now delete B, does that change your answer? – Lasse V. Karlsen Oct 27 '20 at 14:08
  • 2
    Note that git does not store information about which branch a branch was created from, you can only infer what *might've* been by looking at the current repository and figuring out relationships. Also, if you, say, check out master, create and check out a branch from it, make commits on the new branch, merge that into master and then delete the branch, this will be indistinguishable from two people both working on master and then one of them does a pull. – Lasse V. Karlsen Oct 27 '20 at 14:10
  • I'm okay with the best reasonable inference from the current state of the repository, similar to how visual graphs of branches in various git forges work. – pra Oct 27 '20 at 14:16

1 Answers1

0

Start by reading and understanding How to find the nearest parent of a Git branch?

Now, to count the children of some given parent:

  • Use git for-each-ref with refs/heads to loop over all branch names. See the git for-each-ref documentation for details.

  • Use whatever algorithm you come up with, based on this related question, to pair each branch with its purported "parent branch".

  • Count the number of children for your chosen parent, and you're done.

torek
  • 448,244
  • 59
  • 642
  • 775