3

I understand I can get the branches currently unmerged to master in git with

git branch -r --no-merged master

What if I want to see the same for a historical commit but consider the state of branches at that time (including branches that have since been deleted) and not the current branches?

And in the end all I need is a count. Ultimately I'm trying to answer this question:

For each day in the history of a git repo (or for the last X days/months) how many branches existed that were unmerged to master on that day?

Edit: To be clear I only need a count of unmerged branches given a specific commit. I don’t need branch names. I.e. if I could take a cross-section of the log output that shows history graphically and count the lines that represent all branches at any given commit I’d be in good shape. I just don’t want to have to build that out if someone already has.

Edit 2: What about counting commits that are older than the commit in question and have a shared parent and subtract those that have more than one parent. See this diagram: enter image description here

devguydavid
  • 3,917
  • 1
  • 19
  • 18

1 Answers1

1

The branch creation/deletion is not recorded in a Git repo (not to mention you can rename a branch at any time, move it or reset it at any time)

The only record you can find, for a time, is in git reflog (records are pruned after 90 days).

The git repo only keep an history of the commits, and their associated graph (the DAG). Branches are only ephemeral pointers to points in that graph, in order to facilitate working with it.


As for "counting commits" to determine non-merged branches (for a given past commit), you might consider the pseudo-algorithm:

n=0
for each branch b
  if c is part of b
    continue
  fi
  ca=common_ancestor(branch_of_c, b)
  if ca older than c
    n=n+1
  fi
fi

The key part is to use git merge-base in order to find the common ancestor.
If that common ancestor is older than the commit being considered: count the other branch.
If not, do not count it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for responding. I added a clarification to the question. Really all I need is a count, which should be discoverable by following the DAG. My git-fu just isn’t strong enough to know the right approach. – devguydavid Apr 28 '18 at 16:09
  • That is the all point of my answer: you can't. The DAG does not keep branch information. – VonC Apr 28 '18 at 16:13
  • @devguydavid Following your edited question, I propose a pseudo-algorithm to implement that branch count. – VonC Apr 29 '18 at 12:40