Git Calculate the Number of Changed Lines between Commits

To calculate the number of changed lines between two commits in Git, use the git diff command.

It shows the total differences between two commits by listing the files that have been modified, added, or deleted. To view the number of changed lines, use the --shortstat option.

git diff <commit1> <commit2> --shortstat

To view the number of lines changed between multiple commits individually, run git log with the --shortstat option.

git log <commit1> <commit2> --shortstat

This comprehensive guide teaches you how to view the number of changed lines between commits. You will learn how to get the info for individual commits in a range of commits as well as the total number of changed lines.

Let’s jump into it!

Number of Changed Lines with ‘git diff –shortstat’

To use git diff to calculate the number of changed lines between two commits:

  1. Identify the two commits that you want to compare. You can use the git log command to view a list of recent commits and their corresponding commit hashes.
  2. Use the git diff command to compare the two commits. The syntax for this command is as follows:
git diff <commit1> <commit2> --shortstat
  1. Replace <commit1> and <commit2> with the commit hashes for the two commits that you want to compare (or use the HEAD~N to refer to earlier commits)
  2. The output of the git diff command will show the number of changed lines between the two commits.

For example, the output might look something like this:

7 files changed, 65 insertions(+), 33 deletions(-)

This output indicates that 7 files were modified, with 65 lines added and 33 lines removed.

Note: The git diff command only shows the number of changed lines for files that are part of the Git repository. If a file was added or deleted between the two commits, it will not be included in the output of the git diff command.

Example

Let’s see a concrete example to support understanding.

Here’s the commit history of an example project where I modify a test.txt file contents in three different commits:

Now let’s run git diff with the --shortstat option between the first and the last commit:

 $ git diff --shortstat accf83f 4f0338a
1 file changed, 3 insertions(+)

This suggests one file only has changed and there are three total insertions to the file.

This is indeed the case with my example project because now the text file looks like this.

And after the commit accf83f, the file looked like this:

So indeed, there are now 5 lines instead of 2 which means there are 3 additions in total.

Number of Changed Lines with ‘git log –shortstat’

The previous example showed you how to get the number of changed files between two commits by running git diff with the --shortstat option.

The problem with this approach is that it shows the absolute difference but tells nothing about the individual commits.

For example, if you have added some 1000 lines in one commit and then later removed 1000 different lines from the same file the git diff --shortstat shows there are 0 insertions between the commits.

To inspect the number of changed lines in individual commits between a range of commits, you can use git log the --shortstat option. This option will display a summary of the changes made in each commit, including the number of files modified, the number of lines added, and the number of lines removed.

git log --shortstat <commit-1> <commit-2>

If you’re only interested in the number of lines changed, you might find it useful to also use the --oneline option.

git log --oneline --shortstat <commit-1> <commit-2>

The result is an output that might look something like this:

4b7e32d Add new feature
9 files changed, 122 insertions(+), 77 deletions(-)

fa35e12 Fix bug
2 files changed, 14 insertions(+), 8 deletions(-)

e8d9f1c Initial commit
3 files changed, 30 insertions(+)

Example

Here is an example of how to use git log with the --shortstat option to show the number of changed lines between multiple commits. (I’m using the same example project as in the previous example)

More specifically, let’s check the summary of changes made by author “artturijalli” in the 3 most recent commits.

$ git log --author="artturijalli" --shortstat HEAD...HEAD~3 --oneline

Notice how I use the HEAD~N to reference the Nth commit instead of the commit hashes. If you see this notation the first time, it can be useful to refer to commits this way to avoid having to look up the commit hashes.

Here’s the output:

This offers a nice overall summary that reveals the number of files changed, inserted lines, and removed lines.

Summary

Today you learned how to calculate the number of lines changed between commits.

To take home, if you’re interested in the absolute number of lines changed between two commits, use git diff with the --shortstat option.

git diff --shortstat <commit-1> <commit-2>

To get a recap of the number of lines changed for each commit between multiple commits, use git log with the --shortstat option.

git log --shortstat <commit-1> <commit-2>

Thanks for reading. Happy coding!

Read Also

How to List Changed Files between Commits in Git

Scroll to Top