How to Check If a File Is Added in a Git Commit

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file was added in a specific Git commit. You will explore different Git commands to inspect commit details and identify newly added files.

You will start by using git show --name-status to view a concise summary of changes in a commit, specifically looking for the 'A' status indicating an added file. Then, you will learn how to use git diff-tree with relevant options to achieve a similar result, which is particularly useful for scripting and advanced scenarios. Finally, you will differentiate between modified and added files within commit history.


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/commit("Create Commit") git/BasicOperationsGroup -.-> git/diff("Compare Changes") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560024{{"How to Check If a File Is Added in a Git Commit"}} git/status -.-> lab-560024{{"How to Check If a File Is Added in a Git Commit"}} git/commit -.-> lab-560024{{"How to Check If a File Is Added in a Git Commit"}} git/diff -.-> lab-560024{{"How to Check If a File Is Added in a Git Commit"}} git/log -.-> lab-560024{{"How to Check If a File Is Added in a Git Commit"}} end

Run git show --name-status

In this step, we'll explore how to view the details of a specific commit, including which files were changed and how. We'll use the git show command with the --name-status option.

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

cd ~/project/my-time-machine

Now, let's use git show to look at the commit we made in the previous lab. We can use the HEAD alias to refer to the most recent commit:

git show --name-status 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

A       message.txt

Let's break down this output:

  • The first part shows the commit details, similar to what you saw with git log.
  • The line A message.txt is the important part here.
    • A stands for "Added". This tells us that the file message.txt was added in this commit.
    • message.txt is the name of the file that was affected.

The --name-status option is very useful because it gives you a concise summary of the changes introduced by a commit, showing only the names of the files and their status (Added, Modified, Deleted, etc.). This is quicker than looking at the full diff if you just want to know which files were involved.

Understanding how to inspect commits is crucial for navigating your project's history and understanding how it has evolved.

Use git diff-tree for Added Files

In this step, we'll use another command, git diff-tree, to see the changes introduced by a commit. While git show is great for seeing the full commit details and diff, git diff-tree is useful for scripting and more advanced scenarios.

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

We'll use git diff-tree with the -r (recursive) and --name-status options, followed by the commit hash. You can get the commit hash from the output of git log or git show. For our first commit, we can also use HEAD.

git diff-tree -r --name-status HEAD

You should see output similar to this:

a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 A       message.txt

Let's look at the output:

  • a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 is the commit hash.
  • A indicates the status of the file change (Added).
  • message.txt is the name of the file.

This output is very similar to the --name-status part of git show. For a simple commit like our first one (which only added a file), the output is almost identical. However, git diff-tree is more flexible and can be used to compare different trees (snapshots) in your repository, not just commits.

For now, the key takeaway is that both git show --name-status and git diff-tree -r --name-status can show you which files were changed and how in a given commit. This is a fundamental skill for understanding your project's history.

Test Modified vs Added Files

In the previous steps, we saw how git show --name-status and git diff-tree --name-status show an A for an added file. Now, let's see how they behave when a file is modified.

First, make sure you are in the ~/project/my-time-machine directory.

Let's add another line to our message.txt file:

echo "Hello again, Future Me" >> message.txt

The >> operator appends the text to the existing file.

Now, let's check the status of our repository:

git status

You should see output indicating that message.txt has been modified:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   message.txt

no changes added to commit (use "git add" and/or "git commit -a")

Git correctly identifies that the file has been modified.

Now, let's stage this change:

git add message.txt

And check the status again:

git status

The output should now show the change is staged:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   message.txt

Notice that even though we added content, Git tracks this as a modified file because the file already existed in the previous commit.

Finally, let's commit this change:

git commit -m "Add another message to the future"

You should see output confirming the commit:

[master a1b2c3d] Add another message to the future
 1 file changed, 1 insertion(+)

Now we have a new commit. Let's use git show --name-status on the latest commit (HEAD) to see the status:

git show --name-status HEAD

The output will be similar to this:

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

    Add another message to the future

M       message.txt

Notice the M before message.txt. This indicates that the file was Modified in this commit.

Similarly, if you use git diff-tree -r --name-status HEAD, you will also see the M status.

This distinction between A (Added) and M (Modified) is important for understanding the history of your files in Git. A means the file was created in that commit, while M means the file existed before and its content was changed.

Summary

In this lab, we learned how to check if a file was added in a Git commit. We first explored the git show --name-status command, which provides a concise summary of changes in a commit, including the status (like 'A' for Added) and name of affected files. This is a quick way to see which files were involved in a commit without viewing the full diff.

We then introduced the git diff-tree command with the -r and --name-status options as an alternative method for inspecting commit changes, particularly useful for scripting and advanced scenarios. While the full details of this step were not provided, the introduction indicates its purpose is to further explore how to identify added files within a commit using a different Git command.