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.
If you want to get the number of lines from a certain author, try the following code:
git ls-files "*.java" | xargs -I{} git blame {} | grep ${your_name} | wc -l
This works as of cloc 1.68:
cloc --vcs=git
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:
If you want to find the total number of non-empty lines, you could use AWK:
git ls-files | xargs cat | awk '/\S/{x++} END{print "Total number of non-empty lines:", x}'
This uses regex to count the lines containing a non-whitespace character.
The best solution, to me anyway, is buried in the comments of @ephemient's answer. I am just pulling it up here so that it doesn't go unnoticed. The credit for this should go to @FRoZeN (and @ephemient).
git diff --shortstat `git hash-object -t tree /dev/null`
returns the total of files and lines in the working directory of a repo, without any additional noise. As a bonus, only the source code is counted - binary files are excluded from the tally.
The command above works on Linux and OS X. The cross-platform version of it is
git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
That works on Windows, too.
For the record, the options for excluding blank lines,
-w
/--ignore-all-space
, -b
/--ignore-space-change
, --ignore-blank-lines
, --ignore-space-at-eol
don't have any effect when used with --shortstat
. Blank lines are counted.
The answer by Carl Norum assumes there are no files with spaces, one of the characters of IFS
with the others being tab
and newline
. The solution would be to terminate the line with a NULL byte.
git ls-files -z | xargs -0 cat | wc -l