Juan Alonso
Juan Alonso

Reputation: 2350

Is there a way of having git show lines added, lines changed and lines removed?

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.

Upvotes: 211

Views: 176607

Answers (5)

quornian
quornian

Reputation: 10173

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.

Upvotes: 234

Flux
Flux

Reputation: 10950

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).

Upvotes: 32

Falieson
Falieson

Reputation: 2556

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

Upvotes: 16

yhluo
yhluo

Reputation: 1353

  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
    

Upvotes: 118

Daniel Pittman
Daniel Pittman

Reputation: 17232

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.

Upvotes: 5

Related Questions