Git diff to show only lines that have been modified

前端 未结 7 1283
北恋
北恋 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:57

    I think for simple cases the regex can be much shorter and easier to remember, with the caveat that this won't work if you have line changes where the line itself starts with + or -

    $ git diff | grep '^[+|-][^+|-]'
    

    The regex says the line should start with + or -, and the immediately following character should be neither of those. I got the same results whether I escaped the + or not here, btw...


    Example:

    $ cat testfile
    A
    B
    C
    D
    E
    F
    G
    

    Say I change C to X, E to Y, and G to Z.

    $ git diff | grep '^[+|-][^+|-]'
    -C
    +X
    -E
    +Y
    -G
    +Z
    

    Like I said above, though, this is just for most cases. If you pipe that output to a file dout, then try the same regex, it won't work.

    $ git diff dout | grep '^[+|-][^+|-]'
    $
    

    Anyways, hope that helps in your case

提交回复
热议问题