Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

diff

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 .

like image 701
Yuri Aps Avatar asked Nov 30 '25 16:11

Yuri Aps


1 Answers

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.

like image 106
phd Avatar answered Dec 03 '25 12:12

phd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!