How do I list all files in a Git commit without showing diff information?
I need to print a plain list of all files that were part of a specific commit. When I use the following command, it lists the files but also includes unwanted diff information for each file:
git show a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
What Git command or option can I use to get just the list of files modified in a commit, without the diff output?
To list all files in a Git commit without showing diff information, use the --name-only option with the git show command. The correct command would be git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d, which will display only the file paths without any diff content. This option is specifically designed to show just the names of files that were modified in the commit.
Contents
- Basic Solution: Using --name-only
- Alternative Methods
- Combining with Other Options
- Practical Examples
- Advanced File Listing Techniques
Basic Solution: Using --name-only
The most straightforward way to list files in a commit without diff output is to use the --name-only (or -s) option with git show. This option suppresses the diff output and shows only the file paths that were modified in the specified commit.
git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
This command will output something like:
src/main.js
README.md
package.json
The --name-only flag is an alias for --name-status, which shows the status of each file (modified, added, or deleted) along with the filename. If you want just the filenames without any status indicators, you can combine it with --pretty=format:.
Alternative Methods
Using git diff-tree
Another approach is to use git diff-tree with the --name-only option:
git diff-tree --no-commit-id --name-only -r a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
This command:
--no-commit-id: Suppresses the commit ID output--name-only: Shows only the filenames-r: Processes each tree recursively
Using git log
You can also use git log with specific formatting options:
git log --name-only --format=format: a303aa90779efdd2f6b9d90693e2cbbbe4613c1d -1
The -1 option limits the output to just that one commit.
Combining with Other Options
Adding Commit Information
If you want to see the commit message along with the file list:
git show --name-only --format=format:"%H %s%n" a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
This will show the commit hash, subject, and then the file names.
Filtering File Types
You can pipe the output to filter for specific file types:
git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d | grep "\.js$"
This will only show JavaScript files modified in that commit.
Counting Files
To count the number of files modified in a commit:
git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d | wc -l
Practical Examples
Example 1: Basic File List
$ git show --name-only HEAD src/components/Header.js src/components/Footer.js src/utils/helpers.js package.json
Example 2: With Status Indicators
$ git show --name-status HEAD M src/components/Header.js A src/components/Footer.js D src/utils/helpers.js M package.json
The status indicators are:
M= ModifiedA= AddedD= DeletedR= RenamedC= Copied
Example 3: Multiple Commits
To see files from multiple commits:
git show --name-only HEAD~3..HEAD
This shows files modified in the last 3 commits.
Advanced File Listing Techniques
Using Custom Formats
For more control over the output format:
git show --pretty=format:"Commit: %H%nAuthor: %an%nDate: %ad%n%nFiles:" --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
Getting Full Paths
To get full paths instead of relative paths:
git show --name-only --pretty=format: --root a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
Sorting Files
To sort the file list alphabetically:
git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d | sort
Creating a Script
You can create a reusable script or function:
# In your .bashrc or .zshrc
list_commit_files() {
if [ -z "$1" ]; then
echo "Usage: list_commit_files <commit-hash>"
return 1
fi
git show --name-only --pretty=format: "$1"
}
Then use it as:
list_commit_files a303aa90779efdd2f6b9d90693e2cbbbe4613c1d
Sources
- Git Documentation - git show
- Git Documentation - git diff-tree
- Git Documentation - git log
- Pro Git Book - Viewing the Commit History
Conclusion
The most efficient way to list files in a Git commit without diff information is using git show --name-only <commit-hash>. This command provides a clean, simple output of just the file paths modified in that specific commit. For more advanced scenarios, you can combine this option with various flags and formatting options to customize the output according to your needs. Remember that you can always pipe the output to other Unix tools like grep, sort, or wc for further processing and analysis of your commit files.