How do I list all the files in a commit in Git?

To list all files modified in a specific Git commit, use one of these methods:

1. Using git show (Simplest Method)

git show --name-only <commit-hash>

Example:

git show --name-only a1b2c3d

Output:

commit a1b2c3d... (commit message)
Author: John Doe <john@example.com>
Date:   Mon Jan 1 12:00:00 2023 +0000

    Your commit message

file1.txt
dir/file2.py

2. Using git diff-tree (Compact Output)

git diff-tree --no-commit-id --name-only -r <commit-hash>

Example:

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

Output (only filenames):

file1.txt
dir/file2.py

3. Using git log with File List

git log -1 --name-only <commit-hash>

Example:

git log -1 --name-only a1b2c3d

Output:

commit a1b2c3d... (commit message)
Author: John Doe <john@example.com>
Date:   Mon Jan 1 12:00:00 2023 +0000

    Your commit message

file1.txt
dir/file2.py

4. For the Most Recent Commit

Replace <commit-hash> with HEAD:

git show --name-only HEAD

Key Notes

  • Use --name-status instead of --name-only to see action types (e.g., A for added, M for modified):
  git show --name-status a1b2c3d

Output:

  A       file1.txt
  M       dir/file2.py
  • For all files in the repository at that commit (not just changed files), use:
  git ls-tree -r --name-only <commit-hash>

Summary

CommandUse Case
git show --name-only <commit>See commit details + changed files
git diff-tree --name-only -r <commit>Get only filenames (no commit info)
git log -1 --name-only <commit>Commit metadata + files changed

Leave a Reply

Your email address will not be published. Required fields are marked *