How do I get the total number of unique lines changed in a commit? #187456
Replies: 4 comments
-
|
Ah, this comes down to Git’s philosophy and design trade-offs. It’s not that Git couldn’t provide “unique lines changed,” but it chooses not to for several practical reasons: 1. Git treats changes as a series of diff operationsGit fundamentally stores content as snapshots of files, not “line edits.” When you make a commit:
Reasoning: this is simple, unambiguous, and efficient for storage, merging, and patch application. Trying to infer “unique lines changed” would require Git to do extra computation and maintain more metadata about “modified vs new/deleted lines,” which Git doesn’t track natively. 2. Ambiguity in “line changed”Consider this diff: -line a
-line b
+line x
+line y
Git avoids this ambiguity and leaves it to tools or scripts if you want a more human-friendly “lines touched” metric. 3. Performance and simplicityGit is designed for speed at massive scale. Counting “unique lines changed” precisely for every commit could:
4. Workarounds existWhile Git itself doesn’t give “unique lines changed,” you can:
💡 Bottom line: Git prefers to be simple, fast, and unambiguous. “Unique lines changed” is a heuristic interpretation, not something Git can know exactly without extra processing. Hope that helps !! |
Beta Was this translation helpful? Give feedback.
-
|
You’ve hit on the fundamental way Git views the world: Git does not "edit" lines. It only knows how to remove an old version of a line and insert a new one. To Git, an "update" is just a deletion and an addition happening at the same coordinate. If you want to distinguish between a replacement (edit) and a pure addition/deletion, you have to look at the "hunk" structure of the diff rather than the raw integers. The Logic: Why the Math is TrickyGit provides
As you noted, this math treats an edit ( How to Calculate "Actual Lines Changed"To find the number of unique lines in the file that were touched, you can use this logic:
The Formula: $$\text{Actual Lines Impacted} = \max(Additions, Deletions)$$ Applying your examples:Scenario | Add (A) | Del (D) | Git Total (A+D) | Actual Impact (max(A,D)) -- | -- | -- | -- | -- Update 1 line + Add 1 line | 2 | 1 | 3 | 2 Delete 1 line + Add 2 lines | 2 | 1 | 3 | 2 |
Beta Was this translation helpful? Give feedback.
-
|
Yeah this confuses a lot of people at first, you’re not wrong 🙂 What Git shows in commit stats is not “how many lines you changed logically”, it’s just how many lines were added and how many were removed based on a diff. So when you edit one line, Git literally sees it as:
It doesn’t know those two belong to the “same line edit”. -----------------------------------------------------------------------------------------------------------------------------------------------So in your examples Case 1: Case 2: From Git’s point of view, both are just 3 diff operations, so they look identical. -----------------------------------------------------------------------------------------------------------------------------------------------Can Git tell the “real number of lines changed”? Git doesn’t track “this line was modified”, only added vs deleted lines. So there’s no built-in stat that says “2 unique lines were changed”. -----------------------------------------------------------------------------------------------------------------------------------------------If you really want an approximation Some people use a rough idea like:
because a modified line usually shows up as one add + one delete. But this is just a guess and can be wrong if multiple edits happen together. -----------------------------------------------------------------------------------------------------------------------------------------------The main takeaway Think of Git stats as: “how big was the diff needed to turn old code into new code” not “how many lines did the developer conceptually change” -----------------------------------------------------------------------------------------------------------------------------------------------So yeah, what you’re seeing is expected behavior, not a bug. |
Beta Was this translation helpful? Give feedback.
-
|
Git counts additions and deletions, not “logical edits.” So when you modify one line, Git records it as 1 deletion + 1 addition, because internally it sees the old line removed and a new line added. That’s why both of your examples show the same stats (2 additions, 1 deletion) even though the real intent is different. Unfortunately, from standard Git stats alone, you cannot reliably determine the true number of logically changed lines. Git’s diff model works at the text level, not at the semantic “this line was edited” level. If you want a closer estimate:
But strictly speaking, Git itself does not distinguish between “line edited” and “line deleted + new line added.” |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
Hi there, I had a question about how to interpret Git commit stats.
I'm trying to calculate the total number of lines changed in a commit, however IIUC Git is treating each edit as an addition and a deletion (despite changing the same line), making it indistinguishable from a new line being created and a separate one being deleted (which I would consider two lines changed.
i.e. I updated one line, and added another line
Additions = 2
Deletions = 1
Total Changes = 3
Two lines were changed (the new line and the updated line).
In another commit, I deleted one line and added two new lines
Additions: 2
Deletions: 1
Total changes: 3
Three lines were changed
Is there any way from Git stats or otherwise to determine the actual number of lines which were changed?
Beta Was this translation helpful? Give feedback.
All reactions