Use git diff to Inspect Modifications
In this step, we'll learn how to use the git diff
command to see the exact changes you've made in your files. While git status
tells you which files have changed, git diff
shows you what has changed within those files.
First, let's make a change to our message.txt
file. Make sure you are still in the ~/project/my-time-machine
directory.
Open the file using the nano
editor:
nano message.txt
Add a new line to the file, for example:
Hello, Future Me
This is a new line.
Press Ctrl + X
to exit, then Y
to save, and Enter
to confirm the filename.
Now that we've modified the file, let's see how Git sees this change using git status
:
git status
You should see output indicating that message.txt
has been modified:
On branch master
Your branch is up to date with 'origin/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
is modified
and the changes are not staged for commit
. This means we've changed the file, but we haven't told Git to prepare this change for a commit yet.
Now, let's use git diff
to see the specific changes:
git diff
You will 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
+This is a new line.
Let's understand this output:
- Lines starting with
---
and +++
show the original file (a/message.txt
) and the new file (b/message.txt
).
- The line starting with
@@
is called a "hunk header". It shows where the changes are located in the file. -1 +1,2
means that starting from line 1 in the original file, 1 line was removed, and starting from line 1 in the new file, 2 lines were added.
- Lines starting with
-
show lines that were removed.
- Lines starting with
+
show lines that were added.
In our case, we added one line, so you see a line starting with +
.
The git diff
command is incredibly useful for reviewing your changes before staging or committing them. It helps you catch mistakes and ensure you are only including the intended modifications in your commits.
Press q
to exit the diff view and return to the command line.