4

I have a single git commit and would need to get the total number of insertions and deletions.

I know "git show <SHA>" is showing about details of my commit but I am not sure how to get total number of changes only from a particular commit.

git diff --stat <SHA1> <SHA2> also does't work because I should use only a single commit.

Kindly share me if you have any information.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Giri lekkala
  • 57
  • 2
  • 7

3 Answers3

11

You should still be able to use git diff --stat:

git diff --stat <SHA>~ <SHA>
# or, for a global total only:
git diff --shortstat <SHA>~ <SHA>

The ~ refers to the direct parent of <SHA>. You are only using one <SHA>: the commit <SHA>, and its direct parent <SHA>~.

For example:

C:\Users\vonc\prog\git\git>git diff --stat e646ab9cf83025e1000db6ec3c1716f978b099f2~ e646ab9cf83025e1000db6ec3c1716f978b099f2
 po/TEAMS    |    8 +-
 po/ca.po    | 3949 ++++++++++++++++++++++++++++++-------------------------
 po/de.po    | 3776 +++++++++++++++++++++++++++++-----------------------
 po/fr.po    | 3746 +++++++++++++++++++++++++++++-----------------------
 po/git.pot  | 3547 +++++++++++++++++++++++++++----------------------
 po/sv.po    | 3723 +++++++++++++++++++++++++++++-----------------------
 po/vi.po    | 3762 +++++++++++++++++++++++++++++-----------------------
 po/zh_CN.po | 4217 +++++++++++++++++++++++++++++++++--------------------------
 8 files changed, 15007 insertions(+), 11721 deletions(-)

For just the total:

C:\Users\vonc\prog\git\git>git diff --shortstat e646ab9cf83025e1000db6ec3c1716f978b099f2~ e646ab9cf83025e1000db6ec3c1716f978b099f2
 8 files changed, 15007 insertions(+), 11721 deletions(-)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

git show --stat $commitish is a thing. Not sure how you missed it since you saw the other ones.

o11c
  • 15,265
  • 4
  • 50
  • 75
1

Here's a short script which you can enter in terminal:-

git config --global alias.total '!echo $(git log --author="$(git
 config user.name)" --no-merges --before=$(date "+%Y-%m-%dT00:00")  --reverse | grep commit | wc -l) commits, $(git log --author="$(
git config user.name)" --no-merges --before=$(date "+%Y-%m-%dT00:00")  --reverse --stat | grep -Eo "[0-9]{1,} files? changed" | grep
 -Eo "[0-9]{1,}" | awk "{ sum += \$1 } END { print sum }") files changed, $(git log --author="$(git config user.name)" --no-merges -
-before=$(date "+%Y-%m-%dT00:00")  --reverse --stat | grep -Eo "[0-9]{1,} insertions?" | grep -Eo "[0-9]{1,}" | awk "{ sum += \$1 }
END { print sum }") insertions and $(git log --author="$(git config user.name)" --no-merges --before=$(date "+%Y-%m-01T00:00")  --re
verse --stat | grep -Eo "[0-9]{1,} deletions?" | grep -Eo "[0-9]{1,}" | awk "{ sum += \$1 } END { print sum }") deletions'

And use it like this:-

git total

All credit goes to this nice blog post:- http://helpfulsheep.com/2016-03-22-what-git-i-do-last-month/

Ravinder Payal
  • 2,884
  • 31
  • 40