Count Number of Lines in a Git Repository

Count Number of Lines in a Git Repository

September 5, 2024

Saw this at StackOverFlow, I ended up using the following:

git ls-files | xargs wc -l

This will return the lines for each file, with the total at the end. Since I was separating between Python and SQL code, I used *.py and *.sql at the end respectively.

However, a bit below in the comments, there was a way to get a count in your working directory:

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

This will return:

> git diff --shortstat `git hash-object -t tree /dev/null`
 57 files changed, 2787 insertions(+)

While the lines of code does not mean much, it can be helpful to demonstrate certain things. For example, I wanted to give a relative number of a project at work on how much Python and SQL code is required. There was about twice as much Python as their were SQL code to create the reports. However, the code to ingest the data from various source, transform and save to the database, the Python code was less than a quarter to the report program.