Git, a widely-used version control system, offers a variety of commands to help developers navigate and manage their codebase. One such task that often arises is listing all the files in a specific commit. Whether you're reviewing changes, auditing code, or simply curious about a past commit's content, knowing how to list the files can be invaluable. In this Answer, we'll look into the steps to achieve this.
git show
The primary command to list files in a commit is git show
. At its core, git show
displays information about a Git object, be it a commit, tree, or blob.
To list all the files in a specific commit, use:
git show --name-only [commit_hash]
Replace [commit_hash]
with the hash of the commit you're interested in. This will display the commit message and a list of the affected files.
--pretty
If you're only interested in the list of files and want to omit the commit message, you can use the --pretty
option:
git show --pretty="" --name-only [commit_hash]
--stat
For those who want more than just filenames, git show
combined with the --stat
option provides a concise summary:
git show --stat [commit_hash]
This command will display the number of insertions and deletions for each file and a visual representation of the changes.
git diff-tree
for a raw listAnother approach to listing files in a commit is using the git diff-tree
command:
git diff-tree --no-commit-id --name-only -r [commit_hash]
This command provides a raw list of filenames without any additional details, making it suitable for scripting or further processing.
Let's say you have a commit with the hash a1b2c3d4
. To list the files in this commit, you'd run:
git show --name-only a1b2c3d4
The output might look something like:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0Author: Jane Doe <jane.doe@example.com>Date: Mon Sep 12 14:32:21 2022 +0000Added new features and fixed bugsfile1.txtfolder/file2.txtfolder/file3.txt
Git offers a rich set of commands that cater to various needs, and listing files in a commit is just a small fraction of its capabilities. By mastering these commands, developers can gain better insights into their codebase's history and evolution. Whether you're a Git novice or a seasoned pro, understanding how to list files in a commit is a skill that will undoubtedly come in handy in your coding endeavors.
Free Resources