Git
Code Commit
Programming
Version Control
Software Development

How can I calculate the number of lines changed between two commits in Git?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Git can show line changes between two commits in several different formats, and the right command depends on whether you want a human-readable summary or a machine-friendly count. Most of the time, git diff --stat, --shortstat, or --numstat are the key tools.

Choose the Kind of Count You Want

Before picking a command, decide what "number of lines changed" means for your use case:

  • per-file summary
  • total insertions and deletions
  • raw patch output
  • counts that ignore whitespace-only changes

Git can produce all of these, but not with exactly the same command.

Human-Readable Summary with --stat

For a quick overview, use:

bash
git diff --stat COMMIT_A COMMIT_B

Example output:

text
src/app.py     | 10 +++++++---
README.md      |  4 ++--
2 files changed, 8 insertions(+), 6 deletions(-)

This is good when you want to scan the change visually and still see totals.

Just the Totals with --shortstat

If you only care about the aggregate numbers:

bash
git diff --shortstat COMMIT_A COMMIT_B

That produces a compact summary like:

text
2 files changed, 8 insertions(+), 6 deletions(-)

This is often the simplest answer when you want one total for a report or review note.

Machine-Friendly Output with --numstat

For scripting or precise per-file counts, --numstat is more useful.

bash
git diff --numstat COMMIT_A COMMIT_B

Example:

text
7   3   src/app.py
1   3   README.md

The columns mean:

  1. added lines
  2. deleted lines
  3. file path

This format is ideal if you want to sum the counts or filter specific files.

Compute Totals Yourself

If you want exact totals from --numstat, you can sum them:

bash
git diff --numstat COMMIT_A COMMIT_B | awk '{add += $1; del += $2} END {print "added:", add, "deleted:", del, "total:", add + del}'

This is useful in scripts because it turns per-file output into a single final count.

Compare Commit Range Carefully

A common point of confusion is commit ordering. In:

bash
git diff COMMIT_A COMMIT_B

Git shows what changes from COMMIT_A to get to COMMIT_B. The insertion and deletion counts depend on that direction, even though the overall amount of change may look similar.

If you are comparing two commits on the same branch and want the changes introduced after one point, make sure the older commit is first.

Restrict to One File or Directory

You can narrow the count to a path:

bash
git diff --numstat COMMIT_A COMMIT_B -- src/

Or just one file:

bash
git diff --stat COMMIT_A COMMIT_B -- src/app.py

This is very useful when you want counts for one component rather than the entire repository.

Ignore Whitespace Noise

Sometimes line counts are dominated by reformatting rather than real content changes. In those cases, add whitespace-ignore options:

bash
git diff --shortstat -w COMMIT_A COMMIT_B

or:

bash
git diff --shortstat --ignore-space-at-eol COMMIT_A COMMIT_B

This helps when you want a more meaningful content-focused metric.

Example in a Small Repository

Suppose you want totals between two commits:

bash
git log --oneline -2
git diff --shortstat HEAD~1 HEAD
git diff --numstat HEAD~1 HEAD

This gives you both the overall count and the per-file breakdown for the latest commit interval.

Common Pitfalls

Using git show when you actually want a comparison between two arbitrary commits can give the wrong scope of change.

Confusing --stat with --numstat leads to awkward scripting because --stat is designed for humans, not parsers.

Forgetting that commit order affects insertion and deletion direction can make reports misleading.

Counting formatting-only changes as meaningful edits can inflate the numbers unless you use whitespace-ignore options.

Summary

  • Use git diff --stat for a readable summary of changed files and line counts.
  • Use git diff --shortstat for only the aggregate totals.
  • Use git diff --numstat when you need machine-friendly per-file counts.
  • Add a path after -- if you want counts for a specific file or directory.
  • Be explicit about commit order and whitespace handling so the numbers mean what you think they mean.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions