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.
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:
Example output:
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:
That produces a compact summary like:
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.
Example:
The columns mean:
- added lines
- deleted lines
- 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:
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:
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:
Or just one file:
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:
or:
This helps when you want a more meaningful content-focused metric.
Example in a Small Repository
Suppose you want totals between two commits:
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 --statfor a readable summary of changed files and line counts. - Use
git diff --shortstatfor only the aggregate totals. - Use
git diff --numstatwhen 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
- How can I calculate the number of lines changed between two commits in Git?
- How can I change the commit author for a single commit?
- How can I check out a GitHub pull request with git?
- How can I check out a remote Git branch?
- How can I cherry-pick only changes to certain files?
- How can I compare arbitrary version numbers?
- How can I copy the content of a branch to a new local branch?
- How can I create a Git repository with the default branch name other than master?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseTrack 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.