How would I count the total number of lines present in all the files in a git repository?
git ls-files
gives me a list of files tracked by git.
I'm looking for a command to cat
all those files. Something like
git ls-files | [cat all these files] | wc -l
Or:
xargs
will do what you want:But with more information and probably better, you can do:
I've encountered batching problems with
git ls-files | xargs wc -l
when dealing with large numbers of files, where the line counts will get chunked out into multipletotal
lines.Taking a tip from question Why does the wc utility generate multiple lines with "total"?, I've found the following command to bypass the issue:
wc -l $(git ls-files)
Or if you want to only examine some files, e.g. code:
wc -l $(git ls-files | grep '.*\.cs')
This tool on github https://github.com/flosse/sloc can give the output in more descriptive way. It will Create stats of your source code:
This shows the differences from the empty tree to your current working tree. Which happens to count all lines in your current working tree.
To get the numbers in your current working tree, do this:
It will give you a string like
1770 files changed, 166776 insertions(+)
.Try:
on the directory/directories in question