Introduction
In this lab, you will learn how to check for changes in your Git repository. We will explore the essential Git commands git status and git diff to identify modified files and inspect the specific changes made to them.
Through hands-on exercises, you will practice using git status to see which files have been altered and git diff to view the line-by-line differences between the current state of your files and the last committed version. You will also learn how Git indicates files that have not been changed.
Run git status to See Modified Files
In this step, we'll learn how to use git status to see what changes we've made in our project.
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. We'll add a simple line to our message.txt file:
echo "P.S. Hope you're doing well!" >> message.txt
The >> symbol appends the text to the existing file, rather than overwriting it.
Let's check the content of the file to confirm the change:
cat message.txt
You should see:
Hello, Future Me
P.S. Hope you're doing well!
Now, let's see how Git sees this change. Run the git status command:
git status
You should see output similar to 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 (use "git add" and/or "git commit -a")
Git tells us that message.txt has been "modified". This means Git knows the file has changed since the last commit, but the changes haven't been added to the staging area yet.
Understanding git status is crucial because it tells you the current state of your working directory and staging area. It's your main tool for knowing what changes you've made and what's ready to be committed.
Use git diff to Inspect Changes
In the previous step, we saw that git status tells us which files have been modified. But what if we want to see exactly what changes were made? That's where git diff comes in!
git diff shows you the differences between your working directory and the last commit (or the staging area, depending on how you use it). It's like comparing the current version of your file to the version in your last save point.
Let's try it out. Make sure you are still in the ~/project/my-time-machine directory and run:
git diff
You should see output similar to this:
diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+P.S. Hope you're doing well!
Let's break down this output:
diff --git a/message.txt b/message.txt: This line tells us that Git is comparing two versions of themessage.txtfile.index a1b2c3d..e4f5g6h 100644: This is technical information about the file versions.--- a/message.txt: This indicates the original version of the file (before your changes).+++ b/message.txt: This indicates the new version of the file (with your changes).@@ -1 +1,2 @@: This is called a "hunk header". It tells you where the changes are located in the file.-1means one line was removed starting at line 1 (in the original file), and+1,2means two lines are present starting at line 1 (in the new file).+P.S. Hope you're doing well!: Lines starting with a+indicate lines that were added. If you had deleted a line, it would start with a-.
git diff is an incredibly powerful tool. Before you commit your changes, it's always a good idea to run git diff to review exactly what you're about to save. This helps prevent accidentally committing unwanted changes.
Press q to exit the diff view and return to the command line.
Test Unchanged Files
In the previous steps, we saw how git status and git diff work when a file has been modified. But what happens if we run these commands when there are no changes?
Let's find out! Make sure you are in the ~/project/my-time-machine directory.
First, run git status:
git status
Since we haven't made any changes since the last time we checked the status, you should see the same output as before, indicating that message.txt is modified but not staged:
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")
Now, let's try running git diff again:
git diff
You should see the same diff output as before, showing the difference between the current file and the last commit:
diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+P.S. Hope you're doing well!
This confirms that git status and git diff show you the current state of your working directory relative to the last commit, regardless of how many times you run the commands without making further changes.
Now, let's stage the changes we made to message.txt using git add:
git add message.txt
Run git status again:
git status
The output will change to show that the changes are now staged:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: message.txt
Notice that git status now shows "Changes to be committed". This means the changes are in the staging area, ready for the next commit.
What about git diff now? Let's try it:
git diff
This time, git diff will show no output. Why? Because when you run git diff without any arguments, it compares your working directory to the staging area. Since we just added the changes to the staging area, the working directory and the staging area are identical.
To see the difference between the staging area and the last commit, you would use git diff --staged. Let's try that:
git diff --staged
This will show the diff of the changes that are currently in the staging area, which is the line we added:
diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+P.S. Hope you're doing well!
Understanding the difference between git diff (working directory vs. staging area) and git diff --staged (staging area vs. last commit) is a key concept in Git. It helps you manage your changes before you commit them.
Press q to exit the diff view if it appears.
Summary
In this lab, we learned how to check for changes in a Git repository. We started by using git status to identify which files have been modified in the working directory since the last commit. This command provides a summary of the current state of the repository, indicating modified, staged, and untracked files.
Next, we explored git diff to inspect the specific changes made within a modified file. This command shows a line-by-line comparison between the current version of the file in the working directory and the version in the last commit, highlighting additions and deletions. Finally, we confirmed that git status correctly reports no changes when no modifications have been made to files in the repository.



