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 Pyrogue commented Jul 6, 2016

Thank you!
Could not be more simple.

@shic

This comment has been minimized.

Copy link

@shic 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 markhealey commented Dec 8, 2016

Perfect! 🎉

@firedock

This comment has been minimized.

Copy link

@firedock firedock commented Jan 18, 2017

Thanks!

@funilrys

This comment has been minimized.

Copy link

@funilrys 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 dextel2 commented Dec 2, 2017

does this reads the files from sub-directories ?

@Natfan

This comment has been minimized.

Copy link

@Natfan Natfan commented Dec 13, 2017

Yes it does @dextel2.

@codeShaurya

This comment has been minimized.

Copy link

@codeShaurya codeShaurya commented Jan 21, 2018

It's awesome😃

@gnarizzy

This comment has been minimized.

Copy link

@gnarizzy gnarizzy commented Feb 15, 2018

This is great, thanks!

@yusufsn

This comment has been minimized.

Copy link

@yusufsn yusufsn commented Feb 22, 2018

Thank you. It's very helpful

@AlexByte

This comment has been minimized.

Copy link

@AlexByte AlexByte commented Mar 1, 2018

@Ogala

This comment has been minimized.

Copy link

@Ogala Ogala commented Jun 6, 2018

Awesome!

@darrylgrant

This comment has been minimized.

Copy link

@darrylgrant 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 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 tsulatsitamim commented Jul 31, 2018

thank you

@onaseer07

This comment has been minimized.

Copy link

@onaseer07 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 muganwas commented Oct 25, 2018

You're the best.

@hrejali

This comment has been minimized.

Copy link

@hrejali hrejali commented Nov 8, 2018

Perfect!

@xxmatyuk

This comment has been minimized.

Copy link

@xxmatyuk xxmatyuk commented Dec 28, 2018

Lovely!

@SanjeevKumarPandey

This comment has been minimized.

Copy link

@SanjeevKumarPandey SanjeevKumarPandey commented Mar 24, 2019

Super cool!

@mikelyons

This comment has been minimized.

Copy link

@mikelyons mikelyons commented May 2, 2019

❤️

@ajzeffer

This comment has been minimized.

Copy link

@ajzeffer ajzeffer commented Jun 4, 2019

🙇‍♂️

@PhilLab

This comment has been minimized.

Copy link

@PhilLab 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 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 BernieWhite commented Oct 12, 2019

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

@jzadra

This comment has been minimized.

Copy link

@jzadra 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 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

🎉

@rajeshjnu2006

This comment has been minimized.

Copy link

@rajeshjnu2006 rajeshjnu2006 commented Mar 2, 2020

FWIW:

I used the following code to find the number of scripts, and lines of code.
`#########################
import os

all_files = [os.path.join(path, name) for path, subdirs, files in os.walk(os.getcwd()) for name in files if '.py' in name]

print(f'Number of python scripts',len(all_files))
for file in all_files:
print(file)

global_count = 0
for file_path in all_files:
count = len(open(file_path,encoding="utf8").readlines())
global_count = global_count+count

print('Total number of lines of code:',global_count)`

@SandeepAswathnarayana

This comment has been minimized.

Copy link

@SandeepAswathnarayana SandeepAswathnarayana commented Mar 17, 2020

Hi all,

Quick question: Is there a technique where I could count the number of files (under a specific folder) in a GitHub repo?

P.S. I am looking to add a "file counter" to a folder in GitHub repo which has numerous files coming in every day.
Thank you!

@Em-AK

This comment has been minimized.

Copy link

@Em-AK Em-AK commented Mar 17, 2020

@SandeepAswathnarayana it is almost the same command without xargs

This will return the number of all versionned files:
git ls-files | wc -l

you can limit what folders you want to include with:

git ls-files src test | wc -l

you can exclude some folders and/or files with:
git ls-files | grep -Ev 'assets|.jpg|.png' | wc -l

@SandeepAswathnarayana

This comment has been minimized.

Copy link

@SandeepAswathnarayana SandeepAswathnarayana commented Mar 17, 2020

Thanks for reverting, @Em-AK. I want to add a code snippet to the GitHub folder which tracks the number of files in real-time so it's visible for any user on the repo. Do you have any specific suggestions for the same?
Just to make sure I have my intent right: I am not looking to fork the repo or use the cmd line for my personal use on the desktop.

@Em-AK

This comment has been minimized.

Copy link

@Em-AK Em-AK commented Mar 19, 2020

@SandeepAswathnarayana in this case I think it should be possible to run a custom github action on every push to your branch of reference.
In the github action execute the command above, and print the result so that it will be visible in the "Actions" tab of the repo https://help.github.com/en/actions/building-actions/creating-a-docker-container-action

@SandeepAswathnarayana

This comment has been minimized.

Copy link

@SandeepAswathnarayana SandeepAswathnarayana commented Mar 20, 2020

Thanks for the input, @Em-AK. I will give this a try using GitHub 'Actions'. I appreciate your time in this regard. Have a splendid week!

@CodHeK

This comment has been minimized.

Copy link

@CodHeK CodHeK commented Apr 12, 2020

Or, try using this app I built https://github.com/CodHeK/analy.se , you can simply drag n drop your project folder, doesn't store your files on a server so is completely safe! 🥂

@lkozloff

This comment has been minimized.

Copy link

@lkozloff lkozloff commented Apr 23, 2020

In case anyone needs to do this on a bare repository:

git ls-tree --full-tree -r HEAD --name-only | xargs -I '$' git show master:$ | wc -l

is working for me (or at least giving consistent results to the git ls-files version)

@bencamarata

This comment has been minimized.

Copy link

@bencamarata bencamarata commented Apr 27, 2020

This worked perfectly! Thank you so much!

@DrJohnT

This comment has been minimized.

Copy link

@DrJohnT DrJohnT commented May 12, 2020

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

Nice. Thanks for that
git ls-files | %{ Get-Content -Path $_ } | Measure-Object -Line

@alless

This comment has been minimized.

Copy link

@alless alless commented May 15, 2020

What about cloc?

cloc $(git ls-files)

@x-champ

This comment has been minimized.

Copy link

@x-champ x-champ commented May 16, 2020

This is cool ! I want to do this for every commit ever on one of my repositories, is there a way to do that ? How could i loop through every commit ever ? (Only on the master branch)

@neonexus

This comment has been minimized.

Copy link

@neonexus neonexus commented May 24, 2020

This was helpful, but I needed to exclude images, and a few other common files. Thought I would "share with the class":

git ls-files --exclude-standard -- ':!:**/*.[pjs][npv]g' ':!:**/*.ai' ':!:.idea' ':!:**/*.eslintrc' ':!:package-lock.json' | xargs wc -l

':!:**/*.[pjs][npv]g' excludes all png, jpg and svg files.
':!:**/*.ai' excludes .ai files.
':!:.idea' excludes the .idea (WebStorm anyone?) folder.
':!:**/*.eslintrc' excludes .eslint files.
':!:package-lock.json' excludes the biggest offender of all (was over 1/2 my original count).

@TheSnowfield

This comment has been minimized.

Copy link

@TheSnowfield TheSnowfield commented May 26, 2020

Thanks!

@gubareve

This comment has been minimized.

Copy link

@gubareve gubareve commented Jun 17, 2020

Thanks 👍

@alexivkin

This comment has been minimized.

Copy link

@alexivkin alexivkin commented Jul 23, 2020

I made an app to do it completely remotely, on a forked repo, without having to clone it - https://klock.herokuapp.com/

@aviboy2006

This comment has been minimized.

Copy link

@aviboy2006 aviboy2006 commented Aug 11, 2020

Helpful awesome. 👏

@gooznt

This comment has been minimized.

Copy link

@gooznt gooznt commented Sep 17, 2020

🥇 thanks a lot!!

@DanielRomeo

This comment has been minimized.

Copy link

@DanielRomeo DanielRomeo commented Oct 3, 2020

perfect thanks!

@bitansaha

This comment has been minimized.

Copy link

@bitansaha bitansaha commented Oct 7, 2020

Thanks!! 👍

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.