218

git diff --stat and git log --stat show output like:

$ git diff -C --stat HEAD c9af3e6136e8aec1f79368c2a6164e56bf7a7e07 app/controllers/application_controller.rb | 34 +++------------------------- 1 files changed, 4 insertions(+), 30 deletions(-) 

But what really happened in that commit was that 4 lines were changed and 26 lines were deleted which is different than adding 4 lines and deleting 30.

Is there any way of getting the delta LOCs (26 in this case)? I don't really care about differentiating between lines added or removed.

5 Answers 5

243

For per-file numerical diff information:

git diff --numstat 

For aggregated numerical diff information:

git diff --shortstat 

As far as separating modification from an add and remove pair, --word-diff might help. You could try something like this:

MOD_PATTERN='^.+(\[-|\{\+).*$' \ ADD_PATTERN='^\{\+.*\+\}$' \ REM_PATTERN='^\[-.*-\]$' \ git diff --word-diff --unified=0 | sed -nr \ -e "s/$MOD_PATTERN/modified/p" \ -e "s/$ADD_PATTERN/added/p" \ -e "s/$REM_PATTERN/removed/p" \ | sort | uniq -c 

It's a little long-winded so you may want to parse it in your own script instead.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks quornian but numstat gives exactly the same info as stat, additions and deletions.
Updated my answer to include an example use of --word-diff. That might be more useful.
The output of git diff --numstat is broken down by file. To see the total added/removed for the diff, you can pipe it to awk: git diff --numstat | awk '{ added += $1; removed += $2 } END { print "+" added " -" removed }'
@hughes A simpler way of obtaining the same information would be git diff --shortstat. In my experience it is equivalent to accumulating the output of git diff --numstat.
Also FWIW to see the stats for staged changes, simply add --cached
|
118
  1. If you want to know the lines added/changed/deleted by a commit with id commit-id, you could use

    git show commit-id --stat 

    or

    git diff commit-id-before commit-id --stat 
  2. If you wat to know the lines added/changed/deleted by a range commits, you could use

    git diff commit-id1 commit-id2 --stat 
  3. If you want to know the lines added/changed/deleted by each commit, you could use

    git log --stat 

1 Comment

That solution doesn't answer the question, "git --stat" counts a single modified line as "1 insertion and 1 deletion". The question asks how to get "1 changed".
33

You could use diffstat to show the number of modified lines. For example:

git diff HEAD c9af3e6136e8 | diffstat -Cm 

The -C option is for getting colorized output; the -m option is for showing the number of modified lines. Sample output:

 app/controllers/application_controller.rb | 30 -------------------!!! 1 files changed, 0 insertions(+), 26 deletions(-), 4 modifications(!) 

Note that the number of lines in each category (insertions, deletions, modifications) is only approximate, as man diffstat says:

-m merge insert/delete counts from each "chunk" of the patch file to approximate a count of the modified lines.

diffstat has a missing feature when compared to git diff --stat: diffstat is incapable of showing file moves/renames (e.g. app/{a.rb => b.rb}), unlike git diff --stat which is capable of showing this information by using the -M (--find-renames) option or by setting diff.renames in the git configuration file (refer to man git-config).

1 Comment

This needs more upvotes as it actually shows modifications! Thanks
16

If all of your files are staged for commit, to see the --numstat go like this:

git diff --numstat --cached 

example output

32 32 project.pbxproj 

--numstat [...] shows number of added and deleted lines

Comments

5

git uses "unified" diff, which only has added and deleted lines, as the diff format. You have to do something external to get a diff that shows add, delete, and change information.

https://wiki.postgresql.org/wiki/Working_with_Git#Context_diffs_with_Git gives links to a script that allows running regular old "diff" - and from that you can generate a "context" diff output. Context diff does show added, removed, and changed lines, which should allow you to get the data you want.

2 Comments

Voted down for linking to an article instead of reproducing the relevant information here. The article no longer contains the relevant section. (Admittedly, it happens to be a wiki with available edit history, but Stack Overflow answers should stand on their own regardless.)
For anyone who wants to easily get to the content that was removed in an edit, here a link to the old page: wiki.postgresql.org/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.