Sub topic 9
Summarizing a diff: --stat, --name-only, --name-status
A full diff can run to thousands of lines. Often you only need to know which files changed, and by how much. This chapter covers three options that give that summary. It assumes you can read a unified diff and know what git diff compares (working tree, index, HEAD, commits).
Why summarize first
Reading the whole patch of a large change is slow. A summary tells you where to look. You can then open the diff for just the files that matter.
The three options work with git diff, git show, and git log. They change what is printed, not what is compared.
--stat: files and line counts
--stat lists each changed file with the number of lines changed and a bar of + and - characters. The last line totals the change.
git diff --stat
git diff --stat HEAD~1 HEAD
git log --stat -n 3Typical output looks like this:
src/config.c | 14 ++++++++------
src/main.c | 3 ++-
docs/README.md | 8 ++++++++
3 files changed, 18 insertions(+), 7 deletions(-)The number after the pipe is the count of added plus removed lines in that file. A modified line counts as one removal and one insertion. Binary files show Bin with byte sizes instead of line counts.
--name-only: just the paths
--name-only prints one path per line, with no counts.
git diff --name-only main...featureThis output is easy to pass to other tools. For example, you can run a linter only on files that changed:
git diff --name-only --staged | xargs -r ls -lPaths that contain unusual characters may be quoted in the output. Add -z when feeding the list to a script, so entries are separated by NUL bytes instead of newlines.
--name-status: paths with a change type
--name-status adds a status letter before each path.
git show --name-status HEADM src/config.c
A src/util.c
D old/legacy.c
R087 src/net.c src/network.c| Letter | Meaning |
|---|---|
| A | Added |
| M | Modified |
| D | Deleted |
| R | Renamed, followed by a similarity score (0 to 100) |
| C | Copied, followed by a similarity score |
| T | Type changed, for example a file became a symlink |
A rename shows two paths: the old one, then the new one. Git does not record renames. It detects them by comparing content, so R087 means the two files are 87% similar.
Choosing between them
Common errors
- Empty output from
git diff --stat. The default compares the working tree to the index. If you already rangit add, the changes are staged. Use--staged. - Truncated paths.
--statshortens long paths with...to fit the terminal width. Use--stat=200to allow a wider display. - Expecting a rename but seeing D and A. Detection depends on similarity. If the file changed a lot as it moved, Git reports a delete and an add. Adding
-Mwith a lower threshold, such as-M30%, can help. - Line counts that look wrong for a moved block.
--statcounts lines, not meaning. A reformatted file shows a large count even if the logic is unchanged.
Exercise
You merged a teammate's branch feature into main two days ago and want a list of the files it changed, with a letter showing whether each file was added, modified, or deleted. Which command do you run?
Show answer
git diff --name-status main...feature, which compares feature with the merge base of the two branches. Run it before the merge, or use the merge commit's parents afterward. git show --name-status on a merge commit also works, though by default it shows a combined diff that lists only files modified from both parents.
Exercise
git diff --stat prints nothing, but you know you edited files and ran git add. Why, and what do you run instead?
Show answer
The edits are staged, and git diff compares the working tree with the index, so nothing differs. Run git diff --staged --stat to compare the index with HEAD.