How to Check If a File Is Tracked by Git

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file is being tracked by Git. We will explore two essential Git commands: git ls-files and git status.

You will first use git ls-files to list all files currently tracked by Git in your repository. Then, you will use git status to verify the tracking status and understand how Git reports the state of your working directory when all changes are committed. Finally, you will test how Git handles untracked files.


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/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560032{{"How to Check If a File Is Tracked by Git"}} git/status -.-> lab-560032{{"How to Check If a File Is Tracked by Git"}} git/log -.-> lab-560032{{"How to Check If a File Is Tracked by Git"}} end

Run git ls-files to Check File

In this step, we will use the git ls-files command to see which files Git is currently tracking in our repository. This command is useful for getting a quick overview of the files that are part of your project's history.

First, make sure you are in your my-time-machine directory. If you are not, use the cd command to navigate there:

cd ~/project/my-time-machine

Now, run the git ls-files command:

git ls-files

Since we have already created and committed the message.txt file in the previous steps, you should see the name of this file in the output:

message.txt

The git ls-files command lists all the files that are currently tracked by Git in your repository. These are the files that Git is actively monitoring for changes and that are included in your commits.

Understanding which files are tracked is important because Git only manages the history of tracked files. Any files that are not tracked will be ignored by Git when you make commits. This gives you control over what is included in your project's version history.

In the next steps, we will explore how Git handles files that are not yet tracked.

Use git status to Verify Tracking

In the previous step, we used git ls-files to see that message.txt is a tracked file. Now, let's use the git status command again to see how Git reports the state of our repository when all changes are committed.

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

Run the git status command:

git status

You should see output similar to this:

On branch master
nothing to commit, working tree clean

This output tells us a few important things:

  • "On branch master": We are still on the main timeline of our project.
  • "nothing to commit, working tree clean": This is the key part. It means that there are no changes in our working directory that need to be committed. Git sees that the current state of the files matches the state of the last commit.

This is the ideal state for your repository when you have finished a set of changes and committed them. It indicates that your working directory is "clean" and synchronized with the latest commit.

Comparing this output to the git status output we saw before creating message.txt and committing it, you can see how Git's status message changes to reflect the state of your files and commits.

In the next step, we will introduce a new file to see how Git's status changes when there are untracked files present.

Test Untracked Files

In this step, we will create a new file in our my-time-machine directory and see how Git's status changes to reflect the presence of an untracked file.

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

Create a new file called notes.txt using the echo command:

echo "Some random notes" > notes.txt

This command creates a new file named notes.txt and writes the text "Some random notes" into it.

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

git status

You should see output similar to this:

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

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

Notice the "Untracked files:" section. Git has detected the new file notes.txt but is telling us that it is not being tracked. This is because we haven't told Git to include this file in our repository's history yet.

This is a key concept in Git: you have to explicitly tell Git which files you want to track using the git add command. This prevents Git from automatically including every single file in your directory, which might include temporary files, build artifacts, or personal notes that you don't want in your project's history.

In the next lab, we will learn how to add this new file to the staging area and commit it, making it a tracked file in our repository.

Summary

In this lab, we learned how to check if a file is tracked by Git. We used the git ls-files command to list all files currently tracked in the repository, confirming that message.txt was tracked.

We then used the git status command to verify the repository's state. The output "nothing to commit, working tree clean" indicated that all changes were committed and there were no untracked files or pending changes in the working directory. This reinforced our understanding of how Git reports the status of tracked files.