Git diff: is it possible to show ONLY changed lines

21,245

Solution 1

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 $_'

Solution 2

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.

Solution 3

You can use:

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

This will give your output as "+new text"

Solution 4

Here is an answer using grep. It retains the original red/green colors for readability. I provided a few variations in syntax:

git diff --color | grep --color=never $'^\e\[3[12]m'
git diff --color | grep --color=never $'^\033\[3[12]m'
git diff --color | grep --color=never -P '^\e\[3[12]m'
git diff --color | grep --color=never -P '^\033\[3[12]m'

Explanation:

  • git diff --color is needed to prevent git from disabling the color when it is piping.
  • grep --color=never prevents grep from highlighting the matched string (which removes the original color from the original command)
  • Match lines that start with red (\e[31m) or green (\e[32m) escape codes.
  • The $'...' (ANSI-C quoting syntax) or -P (perl syntax) is to let grep to interpret \e or \033 as an ESC character.

Solution 5

A pragmatic approach: Restore the old version (take care not to overwrite your changes), and use good old diff.

Share:
21,245
Artem
Author by

Artem

Software Developer Building Testing and Infrastructure projects

Updated on December 31, 2021

Comments

  • Artem
    Artem over 2 years

    I'm trying to get only new version of lines which have changed and not all the other info which git diff shows.

    For:

    git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix
    

    It shows:

    diff --git file1 file2
    index d9db605..a884b50 100644
    --- file1
    +++ file2
    @@ -16 +16 @@ bla bla bla
    -old text
    +new text
    

    what I want to see is only:

    new text
    

    Is it possible?

  • Martin G
    Martin G over 9 years
    Why is the leading "+ " not trustworthy?
  • michas
    michas over 9 years
    The file header starts with +++ and if you add a line with two plus signs you result in +++, too. You could solve this with more parsing logic but just taking the green lines looks easier.
  • Martin G
    Martin G over 9 years
    Ah, you're completely right. There is no space after the "+" in the git diff output. My mistake.
  • Artem
    Artem over 9 years
    git diff HEAD^^ --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\-[^\-]"
  • Artem
    Artem over 9 years
    Awesome! How do I show deleted versions of the file?
  • Kewei Shang
    Kewei Shang about 4 years
    Use git diff --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'|sed -r 's/\x1B\[m\x1B\[41m[[:blank:]]+//g' to remove trailing spaces color code. If you're using mac OS, replace sed with docker run --rm -i hairyhenderson/sed.
  • Splines
    Splines over 2 years
    Why is this not working for this commit where the lines start with a - sign, but are nevertheless red if you run git diff 104e65a2081^ 104e65a2081?