OMBD#13: Find Out the Total Number of Lines on a Git Repository

A one-liner to count lines, words, and characters on any repo

Jon Portella
2 min readMar 11, 2021

Welcome to issue #13 of One Minute Better Developer, where you become a more successful software developer by reading short nuggets of knowledge, one minute at a time.

⏮️ 🔛 ⏭️

Art by my buddy Loor Nicolas

THE PROBLEM

Our engineering manager has asked us about the total number of lines on several repositories of the company. How can we find out that information?

A SOLUTION

We are going to use the command git ls-files and pipe its output towc using xargs .

  1. As a repository example, we are going to create a new project using superplate.
npx superplate-cli my-app

2. Once created, we are going into the repo directory.

cd my-app

3. Now we can run git ls-files to recursively see all the files versioned in the repo.

git ls-files# .babelrc
# .eslintignore
# .eslintrc
# .gitattributes
# ...

4. However we want to also see the number of lines on every file. We are going to pipe the output of git ls-files to wc using xargs .

git ls-files | xargs wc#       1       4      30 .babelrc
# 1 2 26 .eslintignore
# 25 37 600 .eslintrc
# 1 2 12 .gitattributes
# ...
# 25891 49458 1219057 total

Here we see:

  • 1st column: number of lines in that file.
  • 2nd column: number of words in that file.
  • 3rd column: number of characters in that file.

And in the last row, information on the total of files.

So our repo has 25891 lines.

--

--

Jon Portella

Software Engineer @ Pinterest - Teacher, maker, and general things do-er. - https://www.linkedin.com/in/jonportella/ - Toronto, Canada