
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to List All the Files in a Git Commit
When working with Git, it's often necessary to inspect the contents of a specific commit to understand the changes made. Listing the files in a commit provides clarity, whether you're debugging, reviewing changes, or auditing the project history.
Git offers several commands to list all file in a git commit, each with its own level of detail and utility. This guide explores various methods to list files in a commit, ensuring you can select the best approach for your needs
1. Using git show
The git show command is one of the most straightforward ways to list file changes in a commit. It's simple and quick.Advantages:
- Displays commit metadata (author, date, and message) along with file names.
- Use the --name-only flag to show only filenames or --name-status to see their modification types (added, modified, or deleted).
git show --name-only <commit-hash>
Explanation:
- Replace with the hash of the commit you want to inspect.
- The --name-only flag limits the output to the filenames changed in the commit.
2. Using git diff-tree
- Allows recursive listing of files in subdirectories (-r flag).
- Omission of commit IDs (--no-commit-id) keeps the output clean.
git diff-tree --no-commit-id --name-only -r <commit-hash>
- --no-commit-id: Omits the commit ID from the output.
- --name-only: Shows only the filenames.
- -r: Recursively lists files in subdirectories.
3. Using git diff
- Suitable for listing files changed between two points in history.
- Useful for reviewing changes made in a specific commit relative to its parent.
git diff --name-only <commit-hash>^ <commit-hash>
Explanation:
- <commit-hash>^ : Refers to the parent commit.
- <commit-hash> : The target commit.
- Outputs files modified in the specified commit.
- Works seamlessly when reviewing multiple commits.
- Can be tailored to show information about just one commit or a range of commits.
git log -1 --name-only <commit-hash>
- -1: Limits the output to a single commit.
- --name-only: Displays filenames only.
Example Output:

5. Viewing File Changes with Details
- Combines file listings with the exact modifications made to each file.
- Displays commit metadata for context.
git show <commit-hash>
- Commit metadata (author, date, message).
- Filenames.
- The exact changes made to each file.
Best Practice: Use Aliases for Repeated Commands
If you frequently need to list files in commits, consider setting up Git aliases:
git config --global alias.listfiles "show --name-only"Now you can simply run:
git listfiles <commit-hash>
Conclusion
Understanding how to list files in a Git commit is a valuable skill for effective version control and collaboration. Whether you prefer the simplicity of git show or the flexibility of git diff-tree and git log, each method offers unique advantages depending on your requirements.
For frequent use, creating aliases can streamline the process, saving time and effort. With these tools at your disposal, navigating and analyzing Git commits becomes straightforward and efficient.