Test Modified vs Added Files
In the previous steps, we saw how git show --name-status and git diff-tree --name-status show an A for an added file. Now, let's see how they behave when a file is modified.
First, make sure you are in the ~/project/my-time-machine directory.
Let's add another line to our message.txt file:
echo "Hello again, Future Me" >> message.txt
The >> operator appends the text to the existing file.
Now, let's check the status of our repository:
git status
You should see output indicating that message.txt has been modified:
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 correctly identifies that the file has been modified.
Now, let's stage this change:
git add message.txt
And check the status again:
git status
The output should now show the change is staged:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: message.txt
Notice that even though we added content, Git tracks this as a modified file because the file already existed in the previous commit.
Finally, let's commit this change:
git commit -m "Add another message to the future"
You should see output confirming the commit:
[master a1b2c3d] Add another message to the future
1 file changed, 1 insertion(+)
Now we have a new commit. Let's use git show --name-status on the latest commit (HEAD) to see the status:
git show --name-status HEAD
The output will be similar to this:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (HEAD -> master)
Author: Jane Doe <jane.doe@example.com>
Date: Mon Aug 7 10:05:00 2023 +0000
Add another message to the future
M message.txt
Notice the M before message.txt. This indicates that the file was Modified in this commit.
Similarly, if you use git diff-tree -r --name-status HEAD, you will also see the M status.
This distinction between A (Added) and M (Modified) is important for understanding the history of your files in Git. A means the file was created in that commit, while M means the file existed before and its content was changed.