Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A command to calculate lines of code in all tracked files in a Git repo
// Reference: http://stackoverflow.com/questions/4822471/count-number-of-lines-in-a-git-repository
$ git ls-files | xargs wc -l
@Pyrogue

This comment has been minimized.

Copy link

Pyrogue commented Jul 6, 2016

Thank you!
Could not be more simple.

@shic

This comment has been minimized.

Copy link

shic commented Sep 9, 2016

Tried this command on Mac and got "xargs: wc: Argument list too long" error. Is it because the git repo is too big?

@markhealey

This comment has been minimized.

Copy link

markhealey commented Dec 8, 2016

Perfect! 🎉

@firedock

This comment has been minimized.

Copy link

firedock commented Jan 18, 2017

Thanks!

@funilrys

This comment has been minimized.

Copy link

funilrys commented Feb 15, 2017

@shic @mandiwise you can do the following if it's "too long" :)

git ls-files > listOfFiles
cat listOfFiles | xargs wc -l
@dextel2

This comment has been minimized.

Copy link

dextel2 commented Dec 2, 2017

does this reads the files from sub-directories ?

@Natfan

This comment has been minimized.

Copy link

Natfan commented Dec 13, 2017

Yes it does @dextel2.

@codeShaurya

This comment has been minimized.

Copy link

codeShaurya commented Jan 21, 2018

It's awesome😃

@gnarizzy

This comment has been minimized.

Copy link

gnarizzy commented Feb 15, 2018

This is great, thanks!

@yusufsn

This comment has been minimized.

Copy link

yusufsn commented Feb 22, 2018

Thank you. It's very helpful

@AlexByte

This comment has been minimized.

Copy link

AlexByte commented Mar 1, 2018

@Ogala

This comment has been minimized.

Copy link

Ogala commented Jun 6, 2018

Awesome!

@darrylgrant

This comment has been minimized.

Copy link

darrylgrant commented Jun 26, 2018

Does anyone know if there is a way to exclude certain file types from this command? For example get lines of code for a repo but don't include certain file extensions (i.e. .png, .jpg, etc).
Or maybe just including the file extensions you are interested in (i.e. .js, .scss, etc)

@richardartoul

This comment has been minimized.

Copy link

richardartoul commented Jul 4, 2018

@darrylgrant Just use grep -v <PATTERN_TO_IGNORE I.E to exclude .js files run git ls-files | grep -v ".js" | xargs wc -l

@tsulatsitamim

This comment has been minimized.

Copy link

tsulatsitamim commented Jul 31, 2018

thank you

@onaseer07

This comment has been minimized.

Copy link

onaseer07 commented Sep 11, 2018

What do I need to do if I want to exclude multiple file types? Is it going to be something like this? git ls-files | grep -v .{js,css,html} | xargs wc -l

@muganwas

This comment has been minimized.

Copy link

muganwas commented Oct 25, 2018

You're the best.

@hrejali

This comment has been minimized.

Copy link

hrejali commented Nov 8, 2018

Perfect!

@xxmatyuk

This comment has been minimized.

Copy link

xxmatyuk commented Dec 28, 2018

Lovely!

@SanjeevKumarPandey

This comment has been minimized.

Copy link

SanjeevKumarPandey commented Mar 24, 2019

Super cool!

@mikelyons

This comment has been minimized.

Copy link

mikelyons commented May 2, 2019

❤️

@ajzeffer

This comment has been minimized.

Copy link

ajzeffer commented Jun 4, 2019

🙇

@PhilLab

This comment has been minimized.

Copy link

PhilLab commented Jul 13, 2019

Thank you! However, the console shows a large file list, then 150017 total, then again a large file list and then 38491 total . Somehow, the result is divided into several bins but I cannot figure out why. Nothing special happening in the files listed around the first XXXX total.

I also tried piping into a file like @funilrys did it, but the output was the same

@funilrys

This comment has been minimized.

Copy link

funilrys commented Jul 13, 2019

@PhilLab If it's really like that you can then do something like this ...

git ls-files > listOfFiles
cat listOfFiles | xargs wc -l | grep total | xargs | awk '{lineNumber+=$1}END{print lineNumber}`
@BernieWhite

This comment has been minimized.

Copy link

BernieWhite commented Oct 12, 2019

PowerShell git ls-files | %{ Get-Content -Path $_ } | measure

@jzadra

This comment has been minimized.

Copy link

jzadra commented Dec 18, 2019

I get something liek this for every file:

wc: mining.R: open: No such file or directory

@Em-AK

This comment has been minimized.

Copy link

Em-AK commented Jan 14, 2020

Thank you I remixed it to exclude certain types of files (like images) and folders (like external dependencies living in the repo) and put it in a git alias:

git config --local alias.count "! git ls-files | grep -Ev 'ios/Pods|.jpg|.png|.lock|.jar|.bat|.otf|.woff' | xargs wc -l"

then

git count

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.