Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff: show only removed (not moved) lines

Tags:

git

grep

diff

Git diff has the terrific --color-moved option. So I see that Git can detect when lines are moved, but not actually removed.

I need to detect lines removed but not removed. Simply grepping the output of git diff for lines that start with - will not help because that will capture moved lines as well. Can we either:

  1. Configure git diff to show only the removed lines, but not the moved lines?
  2. Configure git diff --color-moved to use a different leading character for moved lines, so that we could grep for a leading - on the output to find the removed lines?
  3. Grep the output of git diff --color-moved for the (default) red lines, as those are the removed lines?
like image 330
dotancohen Avatar asked Sep 11 '26 09:09

dotancohen


1 Answers

A bit fragile but:

git diff --color-moved=plain --color=always |
perl -pe 's/^\e\[31m-/!/; s/\e\[[0-9;]*m//g'
  • set moved lines to a different colour
  • force colour even in pipes
  • use Perl to:
    • replace the prefix character on deleted lines
    • remove the ANSI escape sequences
like image 194
jhnc Avatar answered Sep 14 '26 00:09

jhnc