Git
version control
commits
line changes
software development

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:

bash
git diff <commit1> <commit2>

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:

bash
git diff --stat <commit1> <commit2>

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:

bash
git diff --shortstat <commit1> <commit2>

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:

bash
git diff --numstat <commit1> <commit2> | awk '{added += $1; removed += $2} END {print "Added lines:", added, "Removed lines:", removed}'
  • git diff --numstat: It provides a list of changes in a tab-separated format &lt;lines_added&gt;&lt;tab&gt;&lt;lines_removed&gt;&lt;tab&gt;&lt;file_name&gt;.
  • 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:

bash
commit1: a1f5cad
commit2: 5d25edf

Running the command:

bash
git diff --numstat a1f5cad 5d25edf | awk '{added += $1; removed += $2} END {print "Added lines:", added, "Removed lines:", removed}'

Might output:

 
Added lines: 42 Removed lines: 18

This indicates that between these commits, 42 lines were added, and 18 were removed.

Table Summarizing Key Commands

CommandDescription
git diff &lt;commit1&gt; &lt;commit2&gt;Show changes between two commits.
git diff --stat &lt;commit1&gt; &lt;commit2&gt;Summary of files changed, with inserted/removed lines.
git diff --shortstat &lt;commit1&gt; &lt;commit2&gt;Show the total number of changes only.
git diff --numstat &lt;commit1&gt; &lt;commit2&gt;Tabulated changes with lines added and removed per file.
git diff --numstat ... with awkCalculate 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:

bash
git diff branch1..branch2

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:

bash
git diff HEAD

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:

bash
git diff --cached

Or to see both staged and working directory changes:

bash
git diff HEAD
git diff --cached

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.


Course illustration
Course illustration

All Rights Reserved.