Get the total number of lines of code in a git repository?

nick asked this question 5 months ago
nick on Feb 18 · Edited

How can I get the total lines of code that are in a given git repository on my machine?

Is there a way to do this via the command line?

6 suggested answers
looper003 on Mar 19

Output the total number of lines in your Git repository:

git ls-files | xargs cat | wc -l

If you remove the cat portion, you'll get a full list of each file with it's total in the output as well:

git ls-files | xargs wc -l

You can also only include files with specific extensions:

git ls-files | grep "\.js$" | xargs wc -l
0 replies
moon_man41 on Mar 19

Try this:

git grep ^ | wc -l

It searches all files versioned by git.

0 replies
coderguy on Mar 19
wc -l $(git ls-files)

And you can have the script include only certain types of files:

wc -l $(git ls-files | grep '.*\.css')
0 replies
yaboy01 on Mar 19

You can use CLOC ("Cound Lines Of Code"). It will give you a breakdown of the different categories of files in your repo and how many lines of code each one has.

git ls-files | xargs cloc

This will output something similar to this:

      48 text files.
      48 unique files.
      15 files ignored. v 1.73  T=0.15 s (223.1 files/s, 46159.0 lines/s)
Language                     files          blank        comment           code
Python                          28           1519            728           4659
YAML                             2              9              2             75
Bourne Shell                     3              6              0             17
make                             1              4              6             10
SUM:                            34           1538            736           4761

You'll need to install cloc before this command works:

brew install cloc
0 replies
rusty1_rusty1 on Mar 19
git diff --shortstat `git hash-object -t tree /dev/null`
0 replies
itsbambi on Mar 19

Get the total number of all versioned files:

git ls-files | wc -l

Limit what folders you want to include:

git ls-files test source | wc -l

Exclude some folders and/or files with:

git ls-files | grep -Ev 'images|.jpg|.png|.gif' | wc -l
0 replies