How to Check If a Git Commit Modified a Specific File

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a specific Git commit modified a particular file. We will explore two different methods to achieve this: using the git show --name-only command for a quick overview of changed files, and utilizing the more fundamental git diff-tree command with relevant options to identify file modifications within a commit.

Through hands-on examples, you will see how these commands provide valuable insights into the changes introduced by a commit, helping you understand the scope and impact of historical commits in your Git repository. We will also test these methods with unmodified files to solidify your understanding.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/diff("Compare Changes") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/diff -.-> lab-560068{{"How to Check If a Git Commit Modified a Specific File"}} git/log -.-> lab-560068{{"How to Check If a Git Commit Modified a Specific File"}} end

Run git show --name-only

In this step, we'll explore how to view information about a specific commit using the git show command. This command is incredibly useful for inspecting the details of a commit, including the changes it introduced.

We'll use the --name-only option with git show. This option tells Git to only show the names of the files that were changed in the commit, which is a quick way to see which files were affected.

First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's use git show with the --name-only option to see the files changed in our last commit. Remember, our last commit was the one where we added message.txt.

git show --name-only HEAD

Here, HEAD is a special pointer in Git that refers to the latest commit in your current branch.

You should see output similar to this:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:00:00 2023 +0000

    Send a message to the future

message.txt

The output shows the commit details (commit hash, author, date, and message) followed by the name of the file that was changed in this commit, which is message.txt.

Using git show --name-only is a quick way to get a summary of which files were modified in a specific commit without seeing the full diff (the actual changes within the files). This is helpful when you just want to know the scope of a commit.

Use git diff-tree for File Changes

In this step, we will explore another way to see the files changed in a commit, using the git diff-tree command. While git show --name-only is a common way, git diff-tree is a more fundamental command that shows the differences between two tree objects (which represent the state of your project at a specific commit).

We will use git diff-tree with the -r and --name-only options. The -r option makes the command recursive, meaning it will look into subdirectories. The --name-only option, similar to git show, will only list the names of the files that are different.

First, ensure you are in the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's use git diff-tree to see the files changed in our latest commit. We need the commit hash (the unique identifier) of the commit. You can get this from the output of git log or git show. For our first commit, you can also use HEAD.

git diff-tree -r --name-only HEAD

You should see output similar to this:

message.txt

This output is simpler than git show --name-only because git diff-tree is primarily focused on showing the differences between trees, not the full commit details. When used with --name-only, it just lists the files that were added, deleted, or modified between the parent of the commit and the commit itself. Since our first commit had no parent, it shows the files added in that commit.

Understanding git diff-tree is useful because it's a lower-level command that other Git commands often use internally. It helps you understand how Git tracks changes between different versions of your project.

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.

Summary

In this lab, we learned how to check which files were modified in a specific Git commit. We explored two primary methods: using git show --name-only and git diff-tree with the -r and --name-only options.

We first used git show --name-only HEAD to quickly see the names of files changed in the latest commit, demonstrating its utility for a concise overview. Subsequently, we introduced git diff-tree, a more fundamental command, and used it with the -r and --name-only options to achieve a similar result, highlighting its underlying mechanism for comparing tree objects. Finally, we tested these commands with commits that did not modify a specific file to confirm their behavior and understanding.