Count Total Lines in a Git Repository
March 11, 2021 • 2 min read
A one-liner to count lines, words, and characters on any repo
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 .
- As a repository example, we are going to create a new project using superplate.
npx superplate-cli my-app
- Once created, we are going into the repo directory.
cd my-app
- Now we can run
git ls-filesto recursively see all the files versioned in the repo.
git ls-files
# .babelrc
# .eslintignore
# .eslintrc
# .gitattributes
# ...
- However we want to also see the number of lines on every file. We are going to pipe the output of
git ls-filestowcusingxargs.
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.