In Git, files are created to perform different operations. A single Git repository contains multiple files with different extensions. Each file contains multiple lines of code in it. Sometimes, users want to calculate the total number of lines in a specific repository or file. For this purpose, Git commands are available to count the number of lines.
This study will discuss:
How to Count/Calculate the Number of Lines in a Specific Git Repository?
To count the number of lines in a specific repository, follow the provided steps.
First, write out the below-listed command and redirect to the specific Git repository:
Then, execute the following command to display the number of lines in the current repository:
Here, the “xargs wc -l” option is used to count the number of lines in each file. In the given output, the first column represents the number of lines in each file, and the list of all files can be seen in the second column. Moreover, “8” is the total number of lines count of all the files:
How to Count/Calculate the Number of Lines in a Specific File?
Suppose the repository contains various files with different extensions like .txt, .py, .php, etc and you want to calculate the number of lines of a desired file. So, Git permits them to calculate the number of lines in any file.
Check out the following examples for demonstration.
Example 1: Show Number of Lines in “.txt” Files
To get the count of lines in the text file, utilize the “*/*.txt *.txt” option along with the previously discussed command:
Here, the first “*/*.txt” option is used to get the number of all text files in the repository and the second “*.txt” option is used to calculate the number of lines in each file that has the “.txt” extension. The below output indicates that three files contain text and each file contains one line only:
Example 2: Show Number of Lines in “.php” Files
Use the “*/*.php *.php” option along with the same command to get the number of lines in the PHP file:
Here, it can be observed that the “file.php” contains five lines:
We have explained the procedures to count/calculate the number of lines in the specific Git repository and file.
Conclusion
Git permits users to count the number of liens in a specific Git repository or file. To get the number of lines in a specific Git repository, the “git ls-files | xargs wc -l” command can be used. Furthermore, utilize the “*/*<file-ext> *<file-ext>” option with the same command to display the number of lines in the specific file. This study explained the methods of counting the number of lines in the specific Git repository and file.