Git diff: is it possible to show ONLY changed lines

If you specifically want only the new text part, then use the following:

git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\+"

This is basically your code, piped into the egrep command with a regex. The regex will filter only lines starting with a plus sign.


You can use:

git diff -U0 <commit-hash> | grep "^\+\""

This will give your output as "+new text"


Only added lines does not make sense in all cases. If you replaced some block of text and you happend to include a single line which was there before, git has to match and guess. - Usually the output of git diff could be used as input for patch afterwards and is therefore meaningful. Only the added lines are not precisely defined as git has to guess in some cases.

If you nevertheless want it, you cannot trust a leading + sign alone. Maybe filtering all the green line is better:

git diff --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'

for only deleted lines filter for all the red lines:

git diff --color=always|perl -wlne 'print $1 if /^\e\[31m-(.*)\e\[m$/'

to inspect the color codes in the output you could use:

git diff --color=always|ruby -wne 'p $_'

Tags:

Git

Diff

Git Diff