Skip to main content
Removed -F' ' as it's redundant.
Source Link
Dan
  • 216
  • 1
  • 7

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertions+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertions+0, "deletions:", sum_deletions+0;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertions+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertions+0, "deletions:", sum_deletions+0;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk '{sum_insertions+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertions+0, "deletions:", sum_deletions+0;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

added 4 characters in body
Source Link
Ed Morton
  • 36k
  • 6
  • 25
  • 60

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertion+=$1;sum_deletions+=$2sum_insertions+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertionsum_insertions+0, "deletions:", sum_deletions;sum_deletions+0;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertion+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertion, "deletions:", sum_deletions;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertions+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertions+0, "deletions:", sum_deletions+0;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats

Source Link
Dan
  • 216
  • 1
  • 7

The other posters have answered how to solve your question directly. However, since you mentioned that you are parsing the result of git diff, I would recommend a slightly different approach.

If you want to use the output of diff in scripts, you can use --numstat instead of --stat.

You will end up with a consistent output, as the purpose of --numstat is for scripts.

When using git diff --stat, you get the following output:

$ git diff main --stat
 [...list of files...]
 5 files changed, 112 insertions(+), 20 deletions(-)

When using git diff --statnum, you get the following output instead:

$ git diff main --statnum
-       -       some/binary/file
15      0       some/file
1       1       some/other/file
29      7       another/file
67      12      yet/another/file

The structure above is simply a three-columns structure. The first column is the number of insertions, the second column is the number of deletions, and the last column is the filename.

You can sum up the columns by piping the command to awk without worrying whether there are or aren't any insertions and/or deletions.

$ git diff main --numstat | awk -F' ' '{sum_insertion+=$1;sum_deletions+=$2}END{print "insertions:", sum_insertion, "deletions:", sum_deletions;}'
insertions: 112 deletions: 20

more links: git-diff manual page / Other diff formats