Skip to content

Instantly share code, notes, and snippets.

@panki
Created September 12, 2023 17:27
Show Gist options
  • Save panki/ba99ff696d9c4738a99d78214d63ea0f to your computer and use it in GitHub Desktop.
Save panki/ba99ff696d9c4738a99d78214d63ea0f to your computer and use it in GitHub Desktop.
Git diff with line numbers
git diff HEAD^ HEAD | awk '
/^@@/ {
gsub(/@@|\+|\-|@@/, "", $0);
split($1, old, ",")
split($2, new, ",")
old_line = old[1]
new_line = new[1]
next
}
{
if ($0 ~ /^(---|\+\+\+)|diff|index/) {
print $0;
}
else if($0 ~ /^-/) {
printf "-%4s %s\n", old_line, substr($0,2);
old_line++;
} else if($0 ~ /^\+/) {
printf "+%4s %s\n", new_line, substr($0,2);
new_line++;
} else {
print " ", $0;
old_line++;
new_line++;
}
}' | grep -e "^[-+]" | grep offset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment