After making changes to your files, and even before staging them with git add
or saving them permanently with git commit
, you'll often want to know exactly what you changed. Did you delete a line by accident? Did you add the correct configuration setting? Git provides a command specifically for this purpose: git diff
. This command is your primary tool for inspecting the differences between various states of your project: your working directory, the staging area, and your commit history.
git diff
OutputBefore we look at specific comparisons, let's understand the output git diff
produces. It typically shows differences in a format known as a "patch", which is a standard way of representing changes between text files. Here's a breakdown of a typical git diff
output:
diff --git a/config.txt b/config.txt
index e69de29..fb8f733 100644
--- a/config.txt
+++ b/config.txt
@@ -1,0 +1,2 @@
+verbose = true
+theme = dark
diff --git a/config.txt b/config.txt
: This header line indicates that a diff is being run using the Git diff format, comparing two versions of the config.txt
file. a/
represents the source (older version), and b/
represents the target (newer version).index e69de29..fb8f733 100644
: This line provides metadata Git uses internally, including hash identifiers for the file versions being compared and file mode information. You can usually ignore this for basic difference viewing.--- a/config.txt
: This line indicates the source file (a/
version).+++ b/config.txt
: This line indicates the target file (b/
version).@@ -1,0 +1,2 @@
: This is the "hunk header". It summarizes the changes within a specific block (or "hunk") of the file.
-1,0
means the change starts at line 1 in the a/
version (source), and the hunk covers 0 lines from that source file (because it was empty).+1,2
means the change starts at line 1 in the b/
version (target), and the hunk consists of 2 lines in the target file.+
: These lines have been added in the b/
version (the newer state).-
: These lines were present in the a/
version but have been removed in the b/
version.Perhaps the most common use of git diff
is to see changes you've made in your working directory that haven't yet been staged. Running the command without any arguments shows these unstaged changes:
git diff
If you've edited a file (say, README.md
) after your last commit or after the last time you staged it with git add
, git diff
will show you the lines you've added or removed compared to the version currently in the staging area (or the last commit if the staging area matches the last commit for that file).
For example, if you modified README.md
and ran git diff
, you might see output like this:
diff --git a/README.md b/README.md
index 1a2b3c4..5d6e7f8 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
# My Project
This is a sample project to demonstrate Git.
-It currently includes basic setup.
+It currently includes basic setup.
+Added a new feature description.
This tells you that the line "Added a new feature description." was added to README.md
in your working directory, but it hasn't been staged yet. If you were to run git add README.md
now, this change would move to the staging area. Running git diff
again immediately after adding would show no output, because your working directory now matches the staging area for that file.
After you've staged some changes using git add
, you might want to review exactly what will be included in the next commit. You can compare the files in the staging area to the last commit using the --staged
(or --cached
) option:
git diff --staged
Or equivalently:
git diff --cached
This command is very useful for a final review before running git commit
. It shows only the changes that are currently staged. If you added the README.md
change from the previous example, git diff --staged
would now show that addition, while git diff
(without --staged
) would be empty (assuming no further edits were made).
git diff
is an indispensable tool for understanding the state of your project at any given moment. It helps you catch mistakes before committing, verify your changes, and generally stay aware of how your project is evolving between snapshots. You'll use it frequently as you work with Git.
© 2025 ApX Machine Learning