git list files changed in commit
Introduction
When you need to see which files were modified in a specific commit, understanding the right Git commands is essential. Because of that, this guide explains how to list files changed in a commit using built‑in tools such as git show, git diff, and git log. By mastering these commands, you can quickly audit changes, troubleshoot bugs, and integrate reliable checks into CI/CD pipelines without leaving the terminal.
Understanding git commit and changes
What is a commit?
A commit is a snapshot of the repository at a given point in time. It records a set of changes to one or more files, along with a message that describes the purpose of the update. Each commit has a unique identifier, called a hash, which you can use to reference it precisely.
How git tracks changes
Git stores the state of the index (staging area) and the working tree. When you run git commit, Git takes the current contents of the index, creates a new tree object, and records a commit object that points to that tree. The differences between the tree of the parent commit and the new tree represent the files changed in commit.
Methods to list files changed in a commit
Using git show
The most straightforward way is git show <commit>. This command displays the commit metadata and a diff of the changes, with each file listed under a header.
git show --name-only
--name-onlytells Git to output only the file names, not the full diff.- The output looks like:
commit abc1234
Author: John Doe
Date: Mon Jan 1 10:00:00 2024 +0000
Add login feature
src/login.js
README.md
Using git diff
git diff compares two commits, a commit and the working tree, or two branches. To list files changed in a commit relative to its parent, you can run:
git diff --name-only ^
- The
^suffix refers to the first parent of the commit, i.e., the snapshot before the changes were made. - This method is useful when you want to see what was added, modified, or deleted across multiple commits.
Using git log with --name-only
If you need a list of files for a range of commits, git log can be combined with --name-only:
git log --pretty=format: --name-only -n 5
--pretty=format:suppresses the commit header, leaving only the file names.-n 5limits the output to the last five commits in the specified range.
Using git diff --stat
For a concise summary that includes the number of insertions and deletions per file, git diff --stat is handy:
git diff --stat ^
- The output shows each file followed by a line like
3 ++indicating three additions.
Practical examples
Example 1: List files in the latest commit
git show --name-only HEAD
HEADpoints to the most recent commit on the current branch.- This command instantly tells you which files were touched in the latest commit.
Example 2: List files changed in a specific commit
git show --name-only abc1234
- Replace
abc1234with the actual commit hash you want to inspect. - The command works whether the commit is on the current branch or on any other branch.
Example 3: Compare two commits and list all files that differ
git diff --name-only abc1234 def5678
abc1234is the older commit,def5678is the newer one.- The result includes every file that was added, removed, or modified between the two snapshots.
Common use cases
Checking what was changed before reverting
When you need to revert a commit, it’s wise to review the affected files first. Running git show --name-only <commit> gives you a quick inventory, helping you decide if a full revert or a selective fix is appropriate.
Auditing code changes
In regulated environments, auditors often request a list of files altered in a specific commit. By using git log --name-only -p <commit> you can generate a detailed report that includes both file names and the exact changes made.
CI/CD pipelines
Automated build systems can incorporate git diff --name-only to determine which files need to be tested or rebuilt after a commit is pushed. This reduces unnecessary compilation and speeds up feedback loops Not complicated — just consistent..
FAQ
How to see renamed files?
Renames are detected automatically by git diff --name-status. Use:
git diff --name-status ^
The output will show R100 for a file renamed from oldname to newname.
Can I list files without checking out the commit?
Yes. Commands like git show --name-only and git diff --name-only operate on the commit object directly, so you never need to checkout the commit’s tree Worth keeping that in mind..
Difference between git show and git diff
git showdisplays the full commit (metadata + diff) for a single commit.git diffcompares two points (e.g., commit vs. parent, or two commits) and can be limited to file names with--name-only.
Conclusion
Knowing how to list files changed in a commit empowers you to audit, debug, and automate your workflow with confidence. By leveraging git show --name-only, git diff --name-only, and related options, you can quickly extract the exact file list you need, whether you’re examining the latest commit, a historical snapshot, or a range of changes. Incorporate these commands into your daily routine, and you’ll streamline code reviews, improve traceability, and keep your development process both transparent and efficient Nothing fancy..