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.