stackyyflow stackyyflow - 1 year ago 148
Git Question

Git log count commits for a specific line in a file

There is a way to retrieve the commits for a specific line in a file as shown below. However, is there a way to count the number of commits for a specific line in a file using git log?

git log -L:40,50:filename.txt

Answer Source

I'd do something like this:

git log -L 1,3:tests/requirements.txt | grep -Ec "commit [0-9a-fA-F]{40}"

What this'll do is grab all the commits that affect those lines, then isolate and count the commit IDs using grep. The -E flag makes grep use extended regexes, and -c makes it output a count instead of the matching lines.

This'll work for any variation of git log - for instance,

git log | grep -Ec "commit [0-9a-fA-F]{40}"

will give you the total number of commits in your repo,

git log tests/requirements.txt | grep -Ec "commit [0-9a-fA-F]{40}"

will give you the total number of commits involving that file, and so on. As long as your git log invocation includes full commit IDs, you're golden.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download