List the files changed by a Git commit

List changed paths or a complete commit snapshot, handle root and merge commits explicitly, and process unusual filenames without splitting them.

Best practices
Illustration of Git branches and version history

Use git show --name-only COMMIT for a readable list of paths changed by an ordinary commit. To list every file in its snapshot, use git ls-tree -r --name-only COMMIT instead. A change list and a snapshot answer different questions.

Choose the output you need

Replace a1b2c3d with an actual commit ID:

git show --name-only --oneline a1b2c3d
git show --stat a1b2c3d
git show --name-status a1b2c3d
git ls-tree -r --name-only a1b2c3d

--stat summarizes changed lines; --name-status includes codes such as A, M, and D. Binary changes need not have line counts.

For a path list without a commit header:

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

--root lets an initial commit be compared with an empty tree. Merge commits need an explicit comparison policy.

Handle filenames safely in scripts

Shell word splitting breaks filenames containing spaces, tabs, or newlines. In Bash, use NUL-delimited output:

while IFS= read -r -d '' file; do
  printf 'Changed path: %s\n' "$file"
done < <(git diff-tree --root --no-commit-id --name-only -r -z a1b2c3d)

To list only deleted paths, add --diff-filter=D. Add -z when feeding that list to a tool that understands NUL delimiters. Deleted paths are absent from the resulting snapshot; do not assume they can be opened as current files.

Compare a merge with its mainline parent

git diff --name-only a1b2c3d^1 a1b2c3d

This reports the merge's net change relative to parent one. Verify the parent order for your workflow. A default combined merge diff is not a complete union of all changes against every parent; an empty default listing does not establish that the merge changed nothing.

Inspect uncommitted changes

git status --short
git diff --name-only
git diff --cached --name-only

These show current status, unstaged tracked changes, and staged changes respectively. Use plain git show a1b2c3d when you need patch content as well as filenames.

Export the committed snapshot

git archive --format=zip --output=files.zip a1b2c3d

This archives the selected commit rather than whichever files happen to be in your working directory. To export selected paths, pass reviewed paths after the tree argument. A pipeline from a change list into zip would read working-tree files, mishandle deleted paths, and might package the wrong revision.

See git-diff-tree, git-show, and git-archive.

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