git diff --numstat command is a powerful and handy tool in Git, which allows you to see the differences between commits, working trees, and index in a summary format.
Before delving into the specifics of
--numstat, let’s first understand
git diff in general. By default,
git diff shows you the detailed, line-by-line changes between two states of your project. For example, it can show you the changes between your current working directory and the last commit, or the changes between two specific commits.
While the detailed output of
git diff is extremely useful, there might be scenarios when you’d want a more succinct summary instead of a full diff. This is where
--numstat comes into play.
--numstat option modifies the output of
git diff to present a summary of the changes between two states, in terms of the number of lines added or deleted. Instead of showing the actual line-by-line changes, it displays three pieces of information for each file that was modified:
- The number of lines added
- The number of lines deleted
- The filename
The format is:
<number-of-lines-added> <number-of-lines-deleted> <filename>
If a new file has been added, the number of lines deleted would be represented as
-. Similarly, if a file has been deleted, the number of lines added would be
For example, consider this output:
12 3 src/main/java/com/example/MyClass.java 5 1 src/main/java/com/example/AnotherClass.java - - src/main/java/com/example/OldClass.java
This tells us that in
MyClass.java, 12 lines were added and 3 lines were deleted, in
AnotherClass.java, 5 lines were added and 1 line was deleted, and
OldClass.java was deleted completely.
--numstat option can be especially useful when you’re dealing with a large number of changes and you want to quickly get an overview of what’s been modified. It can also be used as input to scripts or other tools for further processing or analysis.
Remember, you can combine
--numstat with other
git diff options to narrow down the scope of your diff. For instance,
git diff --numstat HEAD~1..HEAD will show the
--numstat summary for the changes introduced in the latest commit.