Count number of lines in a git repository

后端 未结 16 2660
被撕碎了的回忆
被撕碎了的回忆 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:54

    Depending on whether or not you want to include binary files, there are two solutions.

    1. git grep --cached -al '' | xargs -P 4 cat | wc -l
    2. git grep --cached -Il '' | xargs -P 4 cat | wc -l

      "xargs -P 4" means it can read the files using four parallel processes. This can be really helpful if you are scanning very large repositories. Depending on capacity of the machine you may increase number of processes.

      -a, process binary files as text (Include Binary)
      -l '', show only filenames instead of matching lines (Scan only non empty files)
      -I, don't match patterns in binary files (Exclude Binary)
      --cached, search in index instead of in the work tree (Include uncommitted files)

提交回复
热议问题