Git diff to show only lines that have been modified

前端 未结 7 1294
北恋
北恋 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 09:01

    How to use awk to show just the + and - lines, accounting for any color or text formatting git diff may be outputting:

    Not a single one of the other answers here (including my other answer) will do exactly what you want 100% correctly. This answer, however, will. Here's a 1-liner you can copy and paste into your terminal. I've just made it multiple lines for readability--you can copy-paste it the same either way so I might as well make it readable! It relies on the awk programming language:

    git diff --color=always "$@" | awk '
    # 1. Match and then skip "--- a/" and "+++ b/" lines
    /^(\033\[(([0-9]{1,2};?){1,10})m)?(--- a\/|\+\+\+ b\/)/ {
        next 
    } 
    # 2. Now print the remaining "+" and "-" lines ONLY! Note: doing step 1 above first was required or
    # else those lines would have been matched by this matcher below too since they also begin with 
    # the "+" and "-" symbols.
    /^(\033\[(([0-9]{1,2};?){1,10})m)?[-+]/ {
        print $0 
    }
    ' | less -RFX
    

    Here are its features. All these features, when taken together, solve the shortcomings of every other answer here:

    1. It handles color AND no-color output. That's what this regular expression does: ^(\033\[(([0-9]{1,2};?){1,10})m)?
    2. It handles ALL COLORS and ALL TEXT FORMATTING OPTIONS, including bold, italics, strikethrough, etc, which you can set in your git config settings. That's why the regex above has ;? and {1,10} in it: if it detects the start of a color or text formatting code, it will match up to 10 sequences of these combined ANSI codes.
    3. It does NOT also include lines which begin with @@ and the word diff, as the accepted answer does. If you DO want those lines (which quite frankly, I think are useful :) ), do this instead:

      git diff --unified=0
      

      or

      git diff -U0
      
    4. It shows the output in the same exact way as git diff would: in the less pager with optional color output (-R), and only if the text is > 1 page (-F), and while retaining the current page of text on the screen when you quit (-X).

    It also has the benefit of being powerful and easily configurable since it uses the awk programming language.

    If you are interested in learning awk, here's some resources:

    1. gawk (GNU awk) manual: https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
    2. Study git diffn and the comments therein: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-diffn.sh
      1. If you want git diffn too, which is git diff with line numbers, see here: Git diff with line numbers (Git log with line numbers)
    3. Some awk "hello world" and syntax test examples: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk

    As a bonus, I also wrapped up the above to be used as git diffc, which means "git diff to show ONLY 'c'hanges". Usage is identical to git diff; just use git diffc instead! It supports ALL options. Color is ON by default. To turn it off, simply use git diffc --no-color or git diffc --color=never. See man git diff for details.

    Since I just finished git diffn (a tool to show git diff with line 'n'umbers) last night, writing git diffc was trivial. I figured I better do it now while the knowledge is fresh in my head.

    Install git diffc:

    Follow the instructions at the end of this answer here, except everywhere you see git-diffn in the instructions, use git-diffc instead. That includes in the wget command too. Downloading and installing git diffc is easy: it's just a few commands.

提交回复
热议问题