How does the git diff –patch-with-stat option work?

git diff --patch-with-stat is a powerful Git command that combines two commonly used options: --patch (or -p) and --stat.

Let’s discuss each option separately first:

  • --stat: This option provides a high-level overview of the changes that were made. It shows how many lines were added or deleted in each file. It’s a great way to quickly understand the overall impact of a set of changes. The output of this command consists of the filename, followed by a graphic representation of the number of lines added or deleted, and then the raw numbers of lines added and deleted.
  • --patch (or -p): This option shows the actual changes made to each file in a format known as a ‘patch’. The patch not only includes the line numbers and content of the lines that were changed, but it also provides enough context to understand where those changes were made in relation to the rest of the file. The output of a patch includes the old version and new version of each changed line, along with a few lines of context on either side.

Now, when you use git diff --patch-with-stat, Git combines these two options, providing both the high-level statistical overview of the changes and the detailed patch information. It first presents the statistical data (just like --stat), and then the detailed line-by-line changes (just like --patch).

Here is an example of what you might see when you run git diff --patch-with-stat:

diffCopy code src/example.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/example.c b/src/example.c
index 1234567..7654321 100644
--- a/src/example.c
+++ b/src/example.c
@@ -1,5 +1,6 @@
 int main() {
-    printf("Hello, world!\n");
+    printf("Hello, Git!\n");
+    printf("Goodbye, world!\n");
     return 0;

In the --stat portion at the top, you can see that src/example.c was changed, with 2 lines added (represented by ‘++’) and 1 line removed (represented by ‘-‘). In the --patch portion that follows, you can see exactly what those changes were: the line “Hello, world!” was replaced with “Hello, Git!”, and a new line “Goodbye, world!” was added.

Remember, git diff --patch-with-stat can be used to compare different commits, branches, or even what’s in your working directory vs. your last commit. For example, to see changes between the main branch and a feature branch called feature-branch, you could use git diff --patch-with-stat main feature-branch.