How can I calculate the number of lines changed between two commits in Git?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of software development, keeping track of changes in the codebase is crucial. Git, a distributed version control system, provides powerful tools to manage and view differences between commits. Among its many features is the ability to calculate the number of lines changed between two commits. This metric is particularly useful for code reviews, reporting the scope of changes, and generating statistics for project management. Let’s dive deeper into how you can calculate the number of lines changed between two git commits.
Understanding Diffs in Git
Git's diff command is used to show changes between commits, commit and working tree, etc. By default, git diff shows the differences in terms of lines added and removed. This information can be leveraged to calculate the total number of lines modified.
Basic Git Diff
To compare two commits, you use the following command:
Replace <commit1> and <commit2> with the actual commit hashes you want to compare. The output will display the changes in terms of lines added (+) and lines removed (-).
Counting Lines Changed
To focus on the number of lines changed, i.e., added and removed between two commits, you can combine git diff with tools like grep, awk, or wc.
Using git diff --stat
A simple way to see the summary of changes is by using the --stat option:
The --stat flag provides a concise summary of the files changed, with the number of lines added and removed for each file, along with a summary at the end.
Using git diff --shortstat
If you only need the total numbers without the file breakdown, --shortstat can be very handy:
The output shows three numbers that summarize the changes: insertions, deletions, and the number of files changed.
Extracting Exact Numbers
To extract exact numbers from git diff, you can use regular expressions to filter and sum up the lines:
git diff --numstat: It provides a list of changes in a tab-separated format<lines_added><tab><lines_removed><tab><file_name>.awk: This command processes each line to calculate a running sum of added and removed lines.
Example
Consider you have the following two commit SHAs:
Running the command:
Might output:
This indicates that between these commits, 42 lines were added, and 18 were removed.
Table Summarizing Key Commands
| Command | Description |
git diff <commit1> <commit2> | Show changes between two commits. |
git diff --stat <commit1> <commit2> | Summary of files changed, with inserted/removed lines. |
git diff --shortstat <commit1> <commit2> | Show the total number of changes only. |
git diff --numstat <commit1> <commit2> | Tabulated changes with lines added and removed per file. |
git diff --numstat ... with awk | Calculate total added and removed lines using awk, as shown above. |
Additional Subtopics
What Are Commits?
A commit in Git represents a snapshot of your entire repository at a specific time. Each commit has a unique hash that acts as an identifier.
Comparing Branches
Besides individual commits, you can also compare two branches. The procedure is similar, just replace commit IDs with branch names:
This will show the changes in branch2 since it diverged from branch1.
Comparing with Working Directory
To calculate changes between the latest commit and the working directory, use:
This shows the changes made in your working directory relative to the last commit.
Staged Changes
To see changes in the staging area relative to the last commit, use:
Or to see both staged and working directory changes:
Knowing how to measure code changes between commits is essential for maintaining code quality and understanding project progress. The techniques described here provide versatile ways to scrutinize your Git history and analyze how your code evolves over time.

