Using git diff, how can I get added and modified lines numbers?

前端 未结 12 1953
清酒与你
清酒与你 2020-11-30 22:13

Assuming I have a text file

alex
bob
matrix
will be removed
git repo

and I have updated it to be

alex
new line here
another         


        
相关标签:
12条回答
  • 2020-11-30 22:38

    Configure an external diff tool which will show you the line numbers. For example, this is what I have in my git global config:

    diff.guitool=kdiff3
    difftool.kdiff3.path=c:/Program Files (x86)/KDiff3/kdiff3.exe
    difftool.kdiff3.cmd="c:/Program Files (x86)/KDiff3/kdiff3.exe" "$LOCAL" "$REMOTE"
    

    See this answer for more details: https://stackoverflow.com/q/949242/526535

    0 讨论(0)
  • 2020-11-30 22:40

    Not exactly what you were asking for, but git blame TEXTFILE may help.

    0 讨论(0)
  • 2020-11-30 22:45

    I was looking for a way to output only the lines changed for each file using git diff. My idea was to feed this output to a linter for type checking. This is what helped me

    0 讨论(0)
  • 2020-11-30 22:48

    I use the --unified=0 option of git diff.

    For example, git diff --unified=0 commit1 commit2 outputs the diff:

    *enter image description here*

    Because of the --unified=0 option, the diff output shows 0 context lines; in other words, it shows exactly the changed lines.

    Now, you can identify the lines that start with '@@', and parse it based on the pattern:

    @@ -startline1,count1 +startline2,count2 @@

    Back to the above example, for the file WildcardBinding.java, start from line 910, 0 lines are deleted. Start from line 911, 4 lines are added.

    0 讨论(0)
  • 2020-11-30 22:52

    You can use git diff coupled with shortstat parameter to just show the no of lines changed.

    For the no of lines changed (in a file that's already in the repo) since your last commit

    git diff HEAD --shortstat
    

    It'll output something similar to

    1 file changed, 4 insertions(+)
    
    0 讨论(0)
  • 2020-11-30 22:54

    Here's a bash function I cobbled together:

    echo ${f}:
    for n in $(git --no-pager blame --line-porcelain $1 |
            awk '/author Not Committed Yet/{if (a && a !~ /author Not Committed Yet/) print a} {a=$0}' |
            awk '{print $3}') ; do
        if (( prev_line > -1 )) ; then
            if (( "$n" > (prev_line + 1) )) ; then
                if (( (prev_line - range_start) > 1 )) ; then
                    echo -n "$range_start-$prev_line,"
                else
                    echo -n "$range_start,$prev_line,"
                fi
                range_start=$n
            fi
        else
            range_start=$n
        fi
        prev_line=$n
    done
    if (( "$range_start" != "$prev_line" )) ; then
        echo "$range_start-$prev_line"
    else
        echo "$range_start"
    fi
    

    And it ends up looking like this:

    views.py:
    403,404,533-538,546-548,550-552,554-559,565-567,580-582
    
    0 讨论(0)
提交回复
热议问题