Search code examples
gitdiff

Git diff between commits on a file in the interval line1 up to line2


As I can do on git blame to find the results of line intervals git blame -L 10,23 file.ext .

I wanted to do a gif diff between commits hash1, hash2, on file file.ext, in the interval of lines 10,23.

Something like git diff hash1 hash2 -- file.ext (?) 10,23 .


Solution

  • Both standalone diff and git diff compare files, neither one could extract some portion of files. So the solution is to extract the lines into temporary files, run diff and remove the temporary files. With process substitution in bash that is even simpler:

    diff \
      <(git show hash1:file.ext | head -n23 | tail -n14) \
      <(git show hash2:file.ext | head -n23 | tail -n14)
    

    git show hash:file.ext is used to output a file from given commit. The pipeline head -n23 | tail -n14 extracts first 23 lines and cuts last 14 from it to get lines 10-23.