List changed filenames between two Git commits

Compare commit snapshots, filter by change type, and produce a filename list that scripts can parse safely.

Best practices
Illustration of Git commit history and recovery

Use git diff --name-only A B to list paths whose final contents differ between two commits. Replace A and B with actual commit IDs or branch names. This compares two snapshots; a file changed and then restored between them will not appear.

Choose the output you need

git diff --name-only HEAD~5 HEAD
git diff --name-only main feature-x
git diff --name-status A B
git diff --stat A B

--name-status includes change codes such as A for an addition, M for a modification, and D for a deletion. A detected rename includes both paths. --stat adds a summary of changed lines.

For filters, ask Git directly:

# Paths deleted between the two snapshots.
git diff --name-only --diff-filter=D A B

# Limit the comparison to JavaScript paths.
git diff --name-only A B -- '*.js'

Make script output unambiguous

Ordinary output is convenient to read, but filenames can contain tabs and newlines. Use NUL separators with a NUL-aware consumer:

git diff --name-only -z A B

For human-readable non-ASCII paths, use git -c core.quotePath=false diff --name-only A B. That setting does not make newline-delimited output safe for arbitrary filenames.

Compare a branch with its base

git diff A..B is an endpoint comparison, like git diff A B. Three dots compare the merge base with the second endpoint:

git fetch origin
git diff --name-only origin/main...HEAD

This is useful for reviewing a feature branch or choosing candidate modules for CI. Fetch the required history in shallow clones, and remember that changed-file lists alone cannot account for every build dependency. Release notes can use the same list to describe the affected areas.

Inspect one commit

For an ordinary commit, compare it with its parent. To include an initial commit, use:

git diff-tree --root --no-commit-id --name-only -r COMMIT

Merge commits have multiple parents. Choose the comparison explicitly, for example git diff --name-only COMMIT^1 COMMIT for its first parent. Add -z when feeding either form to a script.

References: git diff, git diff-tree, and Git configuration.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting