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
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).
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
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.I use the following:
This searches all files versioned by git for the regex
^
, which represents the beginning of a line, so this command gives the total number of lines!This works as of cloc 1.68:
cloc --vcs=git
If you want this count because you want to get an idea of the project’s scope, you may prefer the output of CLOC (“Count Lines of Code”), which gives you a breakdown of significant and insignificant lines of code by language.
(This line is equivalent to
git ls-files | xargs cloc
. It usessh
’s$()
command substitution feature.)Sample output:
You will have to install CLOC first. You can probably install
cloc
with your package manager – for example,brew install cloc
with Homebrew.cloc $(git ls-files)
is often an improvement overcloc .
. For example, the above sample output withgit ls-files
reports 471 lines of code. For the same project,cloc .
reports a whopping 456,279 lines (and takes six minutes to run), because it searches the dependencies in the Git-ignorednode_modules
folder.I was playing around with cmder (http://gooseberrycreative.com/cmder/) and I wanted to count the lines of html,css,java and javascript. While some of the answers above worked,
or
pattern in grep didn't - I found here (https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns) that I had to escape itSo this is what I use now:
git ls-files | grep "\(.html\|.css\|.js\|.java\)$" | xargs wc -l
I did this:
this works if you count all text files in the repository as the files of interest. If some are considered documentation, etc, an exclusion filter can be added.