To display the number of lines added or deleted in each commit, aggregated by file, you can use Git's built-in command-line tools. The git log command with certain options can help you achieve this. Here is a method using git log and awk or similar tools to parse the output.
Method 1: Using git log and --stat
1. You can run the following command to see the changes made in each commit, including lines added and deleted for each file:
git log --stat
The output shows each commit, the files changed, and the number of lines added and deleted.
Method 2: Using git log with --numstat
For a more structured output, you can use the --numstat option. This will give you a machine-readable format that you can easily parse:
1. Run the command:
git log --numstat --pretty="%H"
This command will show the commit hash followed by the number of lines added and deleted for each file. The output will look something like this:
<added> <deleted> <file>
<added> <deleted> <file>
2. This output can also be processed with awk to aggregate the additions and deletions by file. Here’s an example command:
git log --numstat --pretty="%H" | awk 'NF==3 { added[$3] += $1; deleted[$3] += $2; } END { for (file in added) print file, added[file], deleted[file] }'
This command uses awk to accumulate the added and deleted lines for each file and prints the result.
Explanation of the Command:
- git log --numstat --pretty="%H": Lists each commit followed by the number of lines added and deleted for each changed file.
- awk 'NF==3 { added[$3] += $1; deleted[$3] += $2; }':
- NF==3: Processes lines that have exactly 3 fields (which correspond to <added>, <deleted>, and <file>).
- added[$3] += $1: Accumulates additions for the file.
- deleted[$3] += $2: Accumul