Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of lines in a git repository

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 
like image 934
Dogbert Avatar asked Jan 27 '11 22:01

Dogbert


People also ask

How do I count the number of lines in a GitHub repository?

If you go to the graphs/contributors page, you can see a list of all the contributors to the repo and how many lines they've added and removed.

How do you count lines of code?

To use cloc simply type cloc followed by the file or directory which you wish to examine. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code. Another cool feature of cloc is that can even be used on compressed files.

How do I count lines of code in bitbucket?

How to count lines of code for the whole Bitbucket instance. It returns the number of lines added and deleted. So, to get the total, you'll simply need to subtract the number of deleted from the added.


2 Answers

git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904 

This shows the differences from the empty tree to your current working tree. Which happens to count all lines in your current working tree.

To get the numbers in your current working tree, do this:

git diff --shortstat `git hash-object -t tree /dev/null` 

It will give you a string like 1770 files changed, 166776 insertions(+).

like image 35
ephemient Avatar answered Oct 05 '22 15:10

ephemient


xargs will let you cat all the files together before passing them to wc, like you asked:

git ls-files | xargs cat | wc -l 

But skipping the intermediate cat gives you more information and is probably better:

git ls-files | xargs wc -l 
like image 161
Carl Norum Avatar answered Oct 05 '22 15:10

Carl Norum