Git diff to show only lines that have been modified

前端 未结 7 1284
北恋
北恋 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条回答
  •  青春惊慌失措
    2020-12-04 08:52

    Following up on Chris' latest comment, the main problem with the post-processing is that you want to keep lines starting with -|+ but you also want to filter out those that start with ---|+++. If you are storing patch files in your repo (I do, in Pydoop), on the other hand, you want to keep lines that start with --|++, so the regexp becomes a bit involved:

    git diff | grep -P '^\+(?:(?!\+\+))|^-(?:(?!--))'
    

    The regexp uses a negative lookahead: see Peter Boughton's answer to this question for a detailed explanation.

    If you do this often, you might want to set up a git alias for it:

    git config --global alias.diffonly '!git diff | grep -P "^\+(?:(?!\+\+))|^-(?:(?!--))"'
    

提交回复
热议问题