I can find all the lines containing "my string" pattern in a single branch of my git repository by following command
git grep "my string" my_branch
Say, it results in following
my_branch: file1:file1 What is "my string"?
my_branch: file2:file2 Hello, "my string" is just "my string"!
We see 3 occurrences in two lines of two files. I can count these lines via
git grep "my string" my_branch | wc -l
It will result in
2
The question is how to get the exact number of string occurrences through all the lines through all the files in a given branch? Is it possible to run some command or script that will give me 3 in my example, not 2?
You can use -o
option in grep
for your requirement and pass it to wc -l
for the count:
Inside man grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
This should work for you:
git grep -o "my string" my_branch | wc -l
Please note that to use -o
option, the git version must be 2.18 or higher.