91

How would you get a count of all the files currently in a git repository?

Dan Rigby
  • 17,133
  • 6
  • 43
  • 60

5 Answers5

169

You can get a count of all tracked files in a git respository by using the following command:

git ls-files | wc -l

Command Breakdown:

  • The git ls-files command by itself prints out a list of all the tracked files in the repository, one per line.
  • The | operator funnels the output from the preceding command into the command following the pipe.
  • The wc -l command calls the word count (wc) program. Passing the -l flag asks it to return the total number of lines.

Note: This returns a count of only the tracked files in the repository meaning that any ignored files or new & uncommitted files will not be counted.

Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
  • 9
    And if you want to count ony for certain file extensions it's easy to pipe through grep like `git ls-files | grep "\.js$" | wc -l` for JavaScript (.js) files – mnorrish Sep 18 '16 at 22:21
  • 1
    In case 1000 files were commited and pushed and then all of them deleted and pushed again, will git ls-files still show those 1000 files? Is there a way to show all the tracked references and also is there a way to show only those files, that are present in the current commit? – infoclogged Jan 10 '18 at 14:06
  • 2
    [this won't work if a file has `'\n'` in its name](https://unix.stackexchange.com/q/128985/44425). You need `git ls-files -z | grep -zc "^"` – phuclv Nov 28 '18 at 02:45
  • 2
    An alternative to using grep to get the count for a file extension as suggested by mnorrish is `git ls-files -x *.js -i | wc -l` the `-x` tells the files to exclude and `-i` tells to only include the excluded files. This works on windows and is less characters to type :) – MrRoboto Jun 06 '19 at 14:56
  • This counts the files in the current branch, is there also some way to count all the files in all branches? – Marco Sep 05 '19 at 12:38
  • As of July 2020, using git version 2.26.2.windows.1, neither `git ls-files` or `git ls-files -z` works for me under the bash v4.4 shell on WIndows 10. – Karl Jul 13 '20 at 17:11
5

Just to build on the accepted answer, you can also filter which types of files you want to count.

Count only .json files

# Will output only json file paths
git ls-files "./*.json" | wc -l

Count only .c files

git ls-files "./*.c" | wc -l

A fairly useful way to gauge what languages are common in a repo...

Ben Winding
  • 10,208
  • 4
  • 80
  • 67
4

If you came here looking for a way to do this for a repo hosted on github without cloning it, you can do this:

svn ls -R https://github.com/exampleproject/branches/master | wc -l
user40176
  • 319
  • 2
  • 10
2

In the github repository view there is no way, you can clone or download the project and use an external tool.

If you are comfortable with command-line tools, you can install git-sizer and run it against a repository you have cloned locally. It can tell you more about your repository than you ever wanted to know.

Check out tokei and git-sizer.

Abu Noman Md Sakib
  • 322
  • 2
  • 5
  • 20
2

This is a solution for Windows using PowerShell

git ls-files | %{ Get-Content -Path $_ } | measure
CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
Tellon
  • 21
  • 2
  • This answer did not work but this one worked for me: `(git ls-files | Measure-Object -line).Lines` – Sahin Jun 03 '22 at 14:21