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.