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).
Best Use Case: When you want a quick overview of the files affected in a specific commit

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.

Example Output:

2. Using git diff-tree

The git diff-tree command provides a more concise and structured output, specifically tailored for listing files. It's highly customizable with several flags.

Advantages:

  • Allows recursive listing of files in subdirectories (-r flag).
  • Omission of commit IDs (--no-commit-id) keeps the output clean.
Best Use Case: When working on scripts or automated tools that need file lists without extra data.

git diff-tree --no-commit-id --name-only -r <commit-hash>
Explanation:
  • --no-commit-id: Omits the commit ID from the output.
  • --name-only: Shows only the filenames.
  • -r: Recursively lists files in subdirectories.

Example Output:

3. Using git diff

The git diff command is highly versatile for comparing changes between commits, branches, or even the working directory.

Advantages:

  • Suitable for listing files changed between two points in history.
  • Useful for reviewing changes made in a specific commit relative to its parent.
Best Use Case: Comparing changes between two specific commits or branches.

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.

4. Using git log with Flags

The git log command, combined with appropriate flags, can show detailed commit history and the files affected.

Advantages:

  • Works seamlessly when reviewing multiple commits.
  • Can be tailored to show information about just one commit or a range of commits.
Best Use Case: Reviewing the history of changes in files over multiple commits.

git log -1 --name-only <commit-hash>
Explanation:
  • -1: Limits the output to a single commit.
  • --name-only: Displays filenames only.

Example Output:

5. Viewing File Changes with Details

For an in-depth look at the changes in a commit, git show without additional flags provides the full diff of the commit.

Advantages:

  • Combines file listings with the exact modifications made to each file.
  • Displays commit metadata for context.
Best Use Case: When both file lists and specific changes are needed for debugging or code review.

git show <commit-hash>
Output 
  • 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.

Geethanjali Gandu
Geethanjali Gandu

Tech Enthusiast

Updated on: 2025-01-06T11:54:17+05:30

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements