How to Check If a File Is Deleted in Git

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a file has been deleted in a Git repository. We will explore different methods to identify deleted files, including using git log to view the history of changes and git status to see the current state of your working directory.

Through hands-on steps, you will create, commit, and then delete a file, observing how Git tracks these changes. You will specifically use git log -- <file> to see the commit where the file was removed and git status to see deleted files before they are committed. Finally, you will test these methods on existing 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/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BasicOperationsGroup -.-> git/rm("Remove Files") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560026{{"How to Check If a File Is Deleted in Git"}} git/status -.-> lab-560026{{"How to Check If a File Is Deleted in Git"}} git/commit -.-> lab-560026{{"How to Check If a File Is Deleted in Git"}} git/rm -.-> lab-560026{{"How to Check If a File Is Deleted in Git"}} git/log -.-> lab-560026{{"How to Check If a File Is Deleted in Git"}} end

Run git log -- File to Check Deletion

In this step, we will explore how to use git log to see the history of changes in our repository, specifically focusing on how it shows file deletions.

First, let's make sure we are in our project directory. Open your terminal and type:

cd ~/project/my-time-machine

Now, let's create a new file that we will later delete. We'll call it to_be_deleted.txt.

echo "This file is temporary." > to_be_deleted.txt

Check that the file was created:

cat to_be_deleted.txt

You should see:

This file is temporary.

Now, let's add this file to the staging area and commit it. This will record its existence in our Git history.

git add to_be_deleted.txt
git commit -m "Add a file to be deleted"

You should see output similar to this, indicating a new commit was created:

[master <commit-id>] Add a file to be deleted
 1 file changed, 1 insertion(+)
 create mode 100644 to_be_deleted.txt

Now, let's delete the file using the rm command:

rm to_be_deleted.txt

The file is now gone from your file system. But what does Git know about this? Let's check the status:

git status

You should see something like this:

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

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

Git correctly identifies that the file has been deleted. This is because Git tracks the state of your files. When a tracked file is removed, Git notices the change.

Now, let's commit this deletion. We use git add again to stage the deletion, and then git commit.

git add to_be_deleted.txt
git commit -m "Delete the temporary file"

You should see output indicating the deletion was committed:

[master <commit-id>] Delete the temporary file
 1 file changed, 1 deletion(-)
 delete mode 100644 to_be_deleted.txt

Finally, let's use git log to see the history, including the deletion.

git log

You will see two commit entries. The most recent one will have the message "Delete the temporary file" and will show that to_be_deleted.txt was deleted.

Press q to exit the log view.

This demonstrates how Git tracks not just the creation and modification of files, but also their deletion, providing a complete history of your project's evolution.

Use git status for Deleted Files

In this step, we will further explore how git status helps us manage deleted files before committing the changes.

Make sure you are still in your project directory:

cd ~/project/my-time-machine

We previously deleted to_be_deleted.txt and committed the deletion. Let's create another file and then delete it without committing the deletion immediately.

Create a new file called another_file.txt:

echo "This is another file." > another_file.txt

Add and commit this new file:

git add another_file.txt
git commit -m "Add another file"

You should see output confirming the commit:

[master <commit-id>] Add another file
 1 file changed, 1 insertion(+)
 create mode 100644 another_file.txt

Now, let's delete another_file.txt using the rm command:

rm another_file.txt

The file is now removed from your file system. Let's check the status of our repository:

git status

This time, the output will look similar to this:

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

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

Notice that Git recognizes the deletion and lists another_file.txt under "Changes not staged for commit". This means Git knows the file is gone, but this change hasn't been prepared to be included in the next commit yet.

This is a crucial point: deleting a file from your file system doesn't automatically stage the deletion in Git. You need to explicitly tell Git that you want to record this deletion in your history.

The git status command is your window into the current state of your repository. It tells you which files have been modified, which are new and untracked, and which have been deleted. By regularly checking git status, you can stay informed about the changes in your project and decide which changes you want to include in your next commit.

In the next step, we will look at how Git handles existing files that are not deleted.

Test Existing Files

In this step, we will see how Git handles modifications to files that are already being tracked.

Ensure you are in your project directory:

cd ~/project/my-time-machine

We have the message.txt file that we created and committed in the first lab. Let's modify this file. We can use the echo command with >> to append text to the existing file.

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

The >> operator appends the text to the end of the file, rather than overwriting it (which > would do).

Let's view the content of the file to confirm the change:

cat message.txt

You should see both lines:

Hello, Future Me
Hello again, Future Me!

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

git status

The output will show something like this:

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 but untracked files present (use "git add" to track)

Git correctly identifies that message.txt has been "modified". It knows this because it compares the current version of the file in your working directory with the version stored in the last commit.

Just like with deleted files, Git tells you that the changes are "not staged for commit". This means you have modified the file, but you haven't yet told Git that you want to include these specific modifications in your next save point.

This is the standard workflow in Git: you make changes to files, then you use git status to see what you've changed, then you use git add to stage the changes you want to commit, and finally, you use git commit to save those staged changes as a new version in your project's history.

Understanding the output of git status is key to working effectively with Git. It provides a clear overview of the state of your project and guides you on the next steps you need to take to record your changes.

Summary

In this lab, we learned how to check if a file has been deleted in Git. We started by creating and committing a file to establish its presence in the repository's history. Then, we deleted the file using the rm command and observed how git status accurately reported the deletion as a change. Finally, we committed the deletion, recording this event in the Git history.

Through these steps, we understood that Git tracks file deletions and provides tools like git status to identify these changes before they are committed. This process demonstrates how Git maintains a record of file lifecycle events, including creation and deletion.