How can you display the changes introduced by each commit, formatted as a summary of additions and deletions?
Posted by RoseHrs
Last Updated: July 01, 2024
To display the changes introduced by each commit in a Git repository as a summary of additions and deletions, you can use the git log command with specific formatting options. Here are some common approaches:
Using git log with --stat
You can use the following command:
git log --stat
This will show each commit along with a summary of changes, including the number of files changed, lines added, and lines deleted.
Using git log with --summary
Alternatively, you can use:
git log --summary
This will provide a summary of the commit along with the overall file changes, showing added and deleted lines.
Custom Formatting with --pretty
If you want a more concise output, you can combine git log with custom formatting and use a specific format for summaries:
git log --pretty=format:"%h - %an, %ar : %s" --numstat
- %h - Commit hash - %an - Author name - %ar - Author date, relative - %s - Subject (commit message) The --numstat option provides a tab-separated output with the number of added and deleted lines alongside the file names affected by each commit.
Example of Custom Command
Here's an example that provides a comprehensive summary of additions and deletions for each commit:
git log --pretty=format:"%h - %an, %ar : %s" --shortstat
This will give you the commit hash, author, time since the commit, and a short summary of changes (including added and deleted lines).
Summary of Additions and Deletions
If you'd like to see a clean summary of just the additions and deletions without detailed file information, you can parse the output using:
git log --pretty=tformat: --numstat | awk '{ add += $1; del += $2 } END { printf "Additions: %s, Deletions: %s\n", add, del }'
This will give you a total count of additions and deletions across all the commits shown.
Conclusion
These commands enable you to display changes introduced by each commit, formatted as a summary of additions and deletions. You can choose the method that best fits your needs depending on the level of detail you require.
Related Content