Count number of lines in a git repository

后端 未结 16 2635
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 03:17

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.

16条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 03:47

    The best solution, to me anyway, is buried in the comments of @ephemient's answer. I am just pulling it up here so that it doesn't go unnoticed. The credit for this should go to @FRoZeN (and @ephemient).

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

    returns the total of files and lines in the working directory of a repo, without any additional noise. As a bonus, only the source code is counted - binary files are excluded from the tally.

    The command above works on Linux and OS X. The cross-platform version of it is

    git diff --shortstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    That works on Windows, too.

    For the record, the options for excluding blank lines,

    • -w/--ignore-all-space,
    • -b/--ignore-space-change,
    • --ignore-blank-lines,
    • --ignore-space-at-eol

    don't have any effect when used with --shortstat. Blank lines are counted.

提交回复
热议问题