Git diff to show only lines that have been modified

前端 未结 7 1232
北恋
北恋 2020-12-04 08:10

When I do a git diff, it shows lines that have been added:

+ this line is added

lines that have been removed:

- this line i         


        
7条回答
  •  -上瘾入骨i
    2020-12-04 08:51

    This answer will retain 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:

    • The git diff --color is needed to prevent git from disabling the color when it is piping.
    • The grep --color=never is to prevent grep removing the original color and highlighting the matched string.
    • We are matching for 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.

提交回复
热议问题