Test Unstaged Files
In this step, we will explore how Git handles changes that have been made to a tracked file but have not yet been added to the staging area. These are called "unstaged" changes.
Make sure you are in the ~/project/my-time-machine
directory.
We previously created and staged future_plans.txt
. Now, let's add another line to this file without staging the change:
echo "Plan 2: Invent a self-folding laundry machine" >> future_plans.txt
The >>
operator appends the text to the existing file, rather than overwriting it.
Now, let's check the status of our repository again:
git status
You should see output similar to this:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: future_plans.txt
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: future_plans.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
message.txt
Notice the new section: "Changes not staged for commit:". This tells us that Git sees changes in future_plans.txt
that are different from the version in the staging area. The file is listed as "modified".
This is a key concept in Git: the working directory (where you make changes) is separate from the staging area. You can have changes in your working directory that are not yet staged.
To see the difference between the working directory and the staging area, you can use git diff
without any options:
git diff
You should see output similar to this:
diff --git a/future_plans.txt b/future_plans.txt
index a1b2c3d..e4f5g6h 100644
--- a/future_plans.txt
+++ b/future_plans.txt
@@ -1 +1,2 @@
Plan 1: Build a bigger time machine
+Plan 2: Invent a self-folding laundry machine
This output shows the difference between the version of future_plans.txt
in the staging area (which only has "Plan 1") and the version in your working directory (which now has both "Plan 1" and "Plan 2"). The +
sign again indicates the added line.
Understanding the difference between staged and unstaged changes, and how to view them with git status
and git diff
, is fundamental to using Git effectively.
Press q
to exit the diff view.