Verify Staged vs Unstaged Changes
In this step, we'll further explore the difference between staged and unstaged changes by modifying a file that is already being tracked by Git.
First, ensure you are in the ~/project/my-time-machine
directory:
cd ~/project/my-time-machine
Now, let's add another line to our notes.txt
file. We can use the echo
command with >>
to append text to an existing file:
echo "Another idea" >> notes.txt
This command adds the line "Another idea" to the end of notes.txt
.
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: notes.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: notes.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
message.txt
Notice that notes.txt
now appears in two sections:
- Changes to be committed: This refers to the version of
notes.txt
that we added to the staging area in the previous step (which only contained "Ideas for the future").
- Changes not staged for commit: This refers to the changes we just made to
notes.txt
(adding "Another idea"). These changes are in our working directory but have not been added to the staging area yet.
This is a key concept in Git: the staging area holds a snapshot of changes that are ready for the next commit, while the working directory contains the current state of your files, including changes that haven't been staged yet.
To see the difference between the working directory and the staging area, you can use the git diff
command without any options:
git diff
This will show you the changes that are not staged. You should see output showing the line "Another idea" being added.
To see the difference between the staging area and the last commit (which we saw in the previous step), you use git diff --cached
.
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. It gives you precise control over what goes into each commit.