Part III: Chapter 3.6

git diff

The Details

While git status tells you that a file has changed, git diff tells you exactly what changed inside of it. It is a utility function that loads any two Git Trees (or files) and computes the line-by-line differences between them.

Comparing Trees

Click the buttons below to see how Git dynamically calculates differences between various locations.

?
?

Diff Output

Working vs Index

Command: git diff

By default, running `git diff` with no arguments compares your Working Directory to the Index (Staging Area). It shows you what changes you have made that you have not staged yet.

Index vs HEAD

Command: git diff --staged

By passing the `--staged` flag, Git compares the Index to the last Commit (HEAD). It shows you exactly what changes you are about to commit.

Commit vs Commit

Command: git diff hash1 hash2

You can pass any two Commit hashes. Git will open both Commit Objects, look at their root Tree Objects, and recursively calculate the differences between every file in the project at those two points in time.

Reading the Output

Git's diff output format comes from standard Unix utilities. It marks lines from the "Left" tree with a minus (`-`) and lines from the "Right" tree with a plus (`+`).

Terminal
$git diff
diff --git a/style.css b/style.css
index 8f9a1..2b3c4 100644
--- a/style.css
+++ b/style.css
@@ -1,3 +1,4 @@
body {
background: black;
- color: white;
+ color: red;
+ padding: 10px;
}