Test with Unmodified Files
In the previous steps, we saw how git show --name-only
and git diff-tree -r --name-only
show the files that were changed in a specific commit. Now, let's see what happens when we run these commands on a commit where no files were modified (which isn't the case for our single commit, but we can simulate the idea).
Since our current repository only has one commit where a file was added, running these commands on that commit will always show message.txt
. To illustrate the concept of showing only modified files, let's imagine we had a commit that didn't change any files (this usually happens with merge commits or commits that only change metadata, but for this exercise, we'll focus on the output when no files are listed).
If you were to run git show --name-only
or git diff-tree -r --name-only
on a commit that didn't modify any files, the output for the file names part would be empty.
Let's re-run the commands we learned to reinforce the concept. Ensure you are in the my-time-machine
directory:
cd ~/project/my-time-machine
Run git show --name-only
again:
git show --name-only HEAD
Output will be similar to:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9
Author: Jane Doe <[email protected]>
Date: Mon Aug 7 10:00:00 2023 +0000
Send a message to the future
message.txt
Now run git diff-tree -r --name-only
again:
git diff-tree -r --name-only HEAD
Output will be:
message.txt
Both commands correctly show message.txt
because that file was introduced in this commit. The key takeaway here is that these commands are designed to list only the files that were changed (added, deleted, or modified) in the specified commit. If a commit doesn't change any files, these commands (specifically the file listing part) would show nothing.
This behavior is important for understanding the scope of changes introduced by a commit. It helps you quickly identify which parts of your project were affected by a particular change.