How to Check If a File Was Modified in a Specific Commit

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a specific file was modified in a particular Git commit. We will explore different Git commands to achieve this.

You will begin by using the git show --name-only command to view the files changed in a commit. Then, you will learn how to use git diff-tree to examine file changes between commits. Finally, you will test these methods with unmodified files to confirm their behavior.


Skills Graph

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

Use git show --name-only Commit

In this step, we'll learn how to use the git show command to get more details about a specific commit. While git log shows us a summary of commits, git show lets us peek inside a single time capsule.

Let's use git show to see the details of our first commit. We'll also add the --name-only option to see only the names of the files that were changed in that commit.

First, make sure you are in your my-time-machine directory:

cd ~/project/my-time-machine

Now, run the git show command with the --name-only option. You can use HEAD to refer to the latest commit:

git show --name-only HEAD

You should see output similar to this:

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

    Send a message to the future

message.txt

Let's break down the output:

  • The first part is similar to what you saw with git log, showing the commit details (commit ID, author, date, and message).
  • The last line, message.txt, is the output of the --name-only option. It tells us that the file message.txt was changed in this commit.

The git show command is incredibly useful for inspecting individual commits. You can use it to see exactly what changes were made in a specific commit, which files were affected, and the commit message associated with those changes. This helps you understand the history of your project in detail.

In the next step, we'll explore another way to see file changes between commits using git diff-tree.

Run git diff-tree for File Changes

In this step, we'll explore another command to see what files were changed in a commit: git diff-tree. This command is often used in scripting and automation, but it's also helpful for understanding how Git tracks changes.

Make sure you are still in the ~/project/my-time-machine directory.

We need the commit ID of our first commit. You can get this by running git log --oneline:

git log --oneline

You should see output like this (your commit ID will be different):

a1b2c3d (HEAD -> master) Send a message to the future

Copy the first 7 characters of the commit ID (e.g., a1b2c3d).

Now, run the git diff-tree command. We'll use the -r option to show changes recursively and the --name-only option to show only the file names. Replace YOUR_COMMIT_ID with the commit ID you copied:

git diff-tree -r --name-only YOUR_COMMIT_ID

For example, if your commit ID was a1b2c3d, you would run:

git diff-tree -r --name-only a1b2c3d

You should see output like this:

message.txt

This command also shows us that message.txt was the file changed in that commit.

While git show --name-only is often more convenient for interactive use, git diff-tree is a powerful command for comparing trees (snapshots of your project) and is frequently used in more advanced Git workflows and scripts. Understanding that Git tracks changes between these snapshots is key to mastering version control.

Test Unmodified Files

In this final step, we'll confirm that Git correctly identifies files that haven't been modified since the last commit. This reinforces the concept that Git only tracks changes.

Make sure you are in the ~/project/my-time-machine directory.

Run the git status command again:

git status

You should see output like this:

On branch master
nothing to commit, working tree clean

This message tells us that there are no changes in our working directory that need to be committed. Git sees that the message.txt file is exactly the same as it was in the last commit.

Now, let's create a new, untracked file to see how Git reacts:

echo "This is a temporary file" > temp.txt

Run git status again:

git status

You should now see:

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        temp.txt

nothing added to commit but untracked files present (use "git add" to track)

Git correctly identifies temp.txt as an untracked file because we haven't told Git to track it yet using git add. This demonstrates that Git is aware of files in your directory but only actively tracks those you've added to the repository.

Finally, let's clean up the temporary file:

rm temp.txt

Run git status one last time:

git status

You should be back to the "nothing to commit, working tree clean" state.

This step highlights how Git helps you manage your project by clearly showing which files have been modified, which are staged for the next commit, and which are untracked. This clear status information is crucial for effective version control.

Summary

In this lab, we learned how to check if a file was modified in a specific commit using Git. We started by using the git show --name-only command to view the details of a commit and list only the names of the files that were changed. This provided a quick way to see which files were affected by a particular commit.

We then explored the git diff-tree command as another method for identifying file changes within a commit. While often used in scripting, understanding git diff-tree helps in comprehending how Git tracks modifications. These commands are valuable tools for inspecting the history of a project and understanding the changes introduced in individual commits.