How to Show Files in Git Commit

Abdul Jabbar Mar 11, 2025 Git Git Commit
  1. Using the git show Command
  2. Using the git diff Command
  3. Using the git log Command
  4. Conclusion
  5. FAQ
How to Show Files in Git Commit

When working with Git, understanding how to list files in a commit is essential for effective version control. Whether you’re collaborating on a team project or managing your own code, knowing which files have changed can help you track progress and troubleshoot issues.

In this tutorial, we’ll explore various methods to show files in a Git commit, providing you with the knowledge to navigate your repository with confidence. By the end of this guide, you’ll be equipped to handle all your Git commit inquiries like a pro. Let’s dive right in!

Using the git show Command

One of the simplest ways to view files in a specific Git commit is by using the git show command. This command displays the content of the commit, including the files that were modified, added, or deleted. To use it, you need the commit hash, which uniquely identifies each commit in your repository.

Here’s how you can do it:

git show <commit_hash>

Replace <commit_hash> with the actual hash of the commit you want to inspect.

Output:

commit 1a2b3c4d5e6f7g8h9i0j
Author: Your Name <your.email@example.com>
Date:   Mon Oct 1 12:34:56 2023 -0700

    Commit message here

diff --git a/file1.txt b/file1.txt
index 83db48f..f735c1a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,4 +1,4 @@
 Hello World
-Old line
+New line
 Another line

The output provides a comprehensive view of the commit, including the author, date, commit message, and the changes made to each file. The diff section shows the specific changes, making it easy to see what has been added or removed.

Using git show is particularly useful when you want to quickly review the changes in a commit without diving deeper into the commit history. It provides a snapshot of what was altered, which is invaluable during code reviews or debugging sessions.

Using the git diff Command

Another effective way to list files in a Git commit is by using the git diff command. This command can compare changes between commits, branches, or the working directory. If you want to see the files modified in a specific commit compared to its parent, you can use the following syntax:

git diff --name-only <commit_hash>^ <commit_hash>

This command will list only the names of the files that were changed in that commit.

Output:

file1.txt
file2.txt

In this example, --name-only instructs Git to display only the file names without the actual changes. This is particularly helpful when you want a quick overview of which files were affected without sifting through the entire commit details.

Additionally, if you want to compare changes between two specific commits, you can use:

git diff --name-only <commit_hash1> <commit_hash2>

This command will show you the files that differ between the two specified commits. It’s a powerful tool for understanding how your project has evolved over time.

Using git diff can be particularly beneficial when you’re analyzing a series of commits and want to understand the progression of changes across multiple files. It allows you to maintain a clear overview of your repository’s history.

Using the git log Command

The git log command is another useful tool for listing files in a commit. While git log primarily displays the commit history, it can be enhanced with additional options to show the files associated with each commit.

To see the files changed in the latest commit, you can use:

git log -1 --name-only

Output:

commit 1a2b3c4d5e6f7g8h9i0j
Author: Your Name <your.email@example.com>
Date:   Mon Oct 1 12:34:56 2023 -0700

    Commit message here

file1.txt
file2.txt

The -1 option tells Git to limit the output to the most recent commit, while --name-only lists the files that were changed. This method is particularly useful if you want to quickly check the latest changes without going through the entire commit history.

If you want to see the files for a specific commit, you can specify the commit hash like this:

git log <commit_hash> --name-only

This command will provide the same output format but for the specified commit, allowing you to easily track changes over time.

Using git log is advantageous when you want to review multiple commits and their associated files in one go. It gives you a broader context of your project’s evolution, making it a valuable tool for both new and experienced developers.

Conclusion

Understanding how to show files in a Git commit is a fundamental skill for anyone working with version control. Whether you choose to use git show, git diff, or git log, each method provides unique insights into your repository’s history. By mastering these commands, you’ll enhance your ability to track changes, collaborate effectively, and maintain a clean codebase. With practice, you’ll find that managing your Git commits becomes second nature, allowing you to focus more on coding and less on navigating the complexities of version control.

FAQ

  1. How can I find the commit hash for a specific commit?
    You can find the commit hash by using the command git log, which lists all commits along with their hashes.

  2. Can I see the files that were deleted in a commit?
    Yes, using the git show <commit_hash> command will display all changes, including deleted files.

  3. Is there a way to view changes made in multiple commits at once?
    Yes, you can use git diff <commit_hash1> <commit_hash2> to see changes between two commits.

  4. What does the --name-only option do in Git commands?
    The --name-only option restricts the output to just the names of the files that were changed, omitting the actual content changes.

  1. Can I list files in a commit without showing the commit details?
    Yes, using git diff --name-only <commit_hash>^ <commit_hash> will show only the file names without additional commit information.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Commit