Git: Finding Exact Line Numbers Changed in Recent Commits

Git: Finding Exact Line Numbers Changed in Recent Commits

Git, Sometimes you need more than just a list of changed files, you need to know exactly which lines were modified.

how to use Git and AWK to extract precise line ranges from the last 5 commits.

git diff HEAD~5..HEAD -U0 | awk '
/^\+\+\+ b\// { file = substr($0, 7) }
/^@@/ { 
    gsub(/.*\+/, "", $3); 
    gsub(/[^0-9,].*/, "", $3); 
    split($3, a, ",");
    start = a[1];
    count = (a[2] ? a[2] : 1);
    end = start + count - 1;
    print file ": " start "-" end
}'

output will be something like:

src/auth.js: 45-47
src/user.js: 123-123
README.md: 89-95
package.json: 12-15

If you don’t want certain files in the output, you can pipe grep exclusion.

git diff HEAD~4..HEAD -U0 | awk '
/^\+\+\+ b\// { file = substr($0, 7) }
/^@@/ { 
    gsub(/.*\+/, "", $3); 
    gsub(/[^0-9,].*/, "", $3); 
    split($3, a, ",");
    start = a[1];
    count = (a[2] ? a[2] : 1);
    end = start + count - 1;
    print file ": " start "-" end
}' | grep -vE "generated"

06. Jun 2025