git diff in three forms
Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI
Why this matters
git status names the files that changed; git diff shows the lines. It is the difference between "README.md modified" and "the word tiny became small on line 3". Reading your own diff before every commit is the single habit that most reduces embarrassing merge requests, and reading other people's diffs is what review is.
The three comparisons
Every diff compares two of the areas from lesson 2.2:
| Command | Compares | Answers |
|---|---|---|
git diff |
working tree vs staging area | What have I changed that is not staged yet? |
git diff --staged (or --cached) |
staging area vs last commit | What will the next commit contain? |
git diff <a> <b> |
commit a vs commit b | What changed between two points in history? |
And git diff HEAD compares the working tree with the last commit, staged or not: everything since your last commit.
Reading a diff
Ana changed one word in README.md and added a question to docs/faq.md. Nothing is staged yet:
$ git diffdiff --git a/README.md b/README.md
index 19ed536..6516ba6 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Trailguide
-A tiny command-line program that prints hiking trail information for the **Northwind Trails** guide.
+A small command-line program that prints hiking trail information for the **Northwind Trails** guide.
It exists so that people learning Git have something realistic to practice on: documentation to edit,
data to update, a small program to run, and tests that can pass or fail.
diff --git a/docs/faq.md b/docs/faq.md
index 9c0d4c8..9a42534 100644
--- a/docs/faq.md
+++ b/docs/faq.md
@@ -30,3 +30,7 @@ Not yet. It is planned for 1.3.
## Can I add my own trail?
Yes: add a line to data/trails.csv.
+
+## Can I use the data elsewhere?
+
+Yes, the CSV is public domain.| Part | Meaning |
|---|---|
diff --git a/README.md b/README.md |
One block per file; a/ is the old version, b/ the new |
index 19ed536..6516ba6 100644 |
Internal ids of the two versions and the file mode; safe to ignore |
--- a/… / +++ b/… |
Which side is which |
@@ -1,6 +1,6 @@ |
A hunk header: old lines 1–6 became new lines 1–6. A file can have several hunks |
| lines starting with a space | Context: unchanged lines around the change |
- line |
Removed |
+ line |
Added |
A changed line appears as a - line followed by a + line, because Git compares whole lines. The second hunk, @@ -30,3 +30,7 @@, shows three old lines becoming seven: four added, nothing removed. The text after the @@ is Git's guess at the nearest heading, to help you orient.
The three forms in action
Stage the README and compare again:
$ git add README.md
$ git diffdiff --git a/docs/faq.md b/docs/faq.md
index 9c0d4c8..9a42534 100644
--- a/docs/faq.md
+++ b/docs/faq.md
@@ -30,3 +30,7 @@ Not yet. It is planned for 1.3.
## Can I add my own trail?
Yes: add a line to data/trails.csv.
+
+## Can I use the data elsewhere?
+
+Yes, the CSV is public domain.The README vanished from git diff: working tree and staging area agree about it now. It moved to the other side:
$ git diff --stageddiff --git a/README.md b/README.md
index 19ed536..6516ba6 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Trailguide
-A tiny command-line program that prints hiking trail information for the **Northwind Trails** guide.
+A small command-line program that prints hiking trail information for the **Northwind Trails** guide.
It exists so that people learning Git have something realistic to practice on: documentation to edit,
data to update, a small program to run, and tests that can pass or fail.
This is exactly what git commit would record right now. And everything since the last commit, regardless of staging:
$ git diff HEAD --stat README.md | 2 +-
docs/faq.md | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)--stat summarizes instead of listing lines: files, and a bar of + and -. Use it first on big changes, then read the files that matter.
Word-level diffs
For prose, whole-line diffs hide the actual edit. --word-diff marks words instead:
$ git diff --staged --word-diffA [-tiny-]{+small+} command-line program that prints hiking trail information for the **Northwind Trails** guide.[-removed-] and {+added+}. Documentation writers use this constantly; GitLab and GitHub offer the same as a toggle in their diff views.
Between commits
Any two commits, branches or tags can be compared, optionally for one file:
$ git diff HEAD~1 HEAD # the last commit's change
$ git diff v1.2.0 main --stat # everything since the release, summarized
$ git diff HEAD~3 HEAD -- docs/faq.md # one file across three commitsThe -- separates commits from paths, so Git never confuses a file called main with the branch.
In the tools
Everything above. Two more useful flags: git diff --color-words is a lighter word diff; git diff --stat for the summary. Long diffs open in the pager: Space scrolls, q quits.
- In Source Control, click a file under Changes: the diff editor opens with the old version on the left and the new on the right (this is
git difffor that file). Click a file under Staged Changes forgit diff --staged. - In the editor, changed lines have coloured markers in the gutter (green added, blue modified, red triangle deleted); click a marker to see the old text inline.
- Toggle Inline View (the icon at the top right of the diff editor) for a
-/+style; the Command Palette has Git: Open Changes and Git: Open Changes with HEAD. - For commit-to-commit diffs, the Source Control Graph (in the Source Control view) or the Timeline at the bottom of the Explorer: select a commit to see its files.
- In the Commit tool window, select a file and press Ctrl+D (⌘D) or double-click: a side-by-side diff of working tree vs last commit (with the staging area enabled, staged and unstaged are separate nodes).
- In the editor, changed lines are marked in the left gutter; click a marker for the old text and a Revert arrow.
- Git → Show Diff (or the diff icon on a file in the Project view) compares with the last commit; in the Log tab, select two commits and press Ctrl+D to compare them.
- The diff viewer's settings gear offers Highlight words for word-level changes and Ignore whitespace.
GitLab cannot see your uncommitted changes, so git diff and git diff --staged have no equivalent. For committed changes: open a commit (Code → Commits) to see its diff; a merge request's Changes tab is git diff main...branch for the whole branch; Code → Compare revisions lets you pick any two branches or tags. Each diff view has a toggle for inline or side-by-side and a Show whitespace changes option.
Same limits and same views: a commit page shows its diff; a pull request's Files changed tab shows the whole branch; https://github.com/org/repo/compare/v1.2.0...main compares any two points. The gear on a diff view offers Split/Unified and Hide whitespace; word-level highlighting is automatic for changed lines.
Common mistakes
git diffshows nothing, so "there are no changes". They may be staged.git diff --staged, orgit diff HEADfor both.- Reading the header lines as changes. Only
-and+lines are changes. - Whole-file diffs from reformatting or line endings.
--statreveals it (hundreds of lines for a one-word edit); lesson 4.5 explains. - Forgetting
--before a path that looks like a branch name.
Try it yourself
Goal: see the same change move from one diff form to the other.
- In the playground, change one word in
README.mdand add two lines todocs/faq.md. - Run
git diff --stat, thengit diff. - Stage the README only; run
git diff --statandgit diff --staged --stat. - Run
git diff --staged --word-diffto see the changed word.
Expected result: step 2 lists both files; after staging, git diff lists only the FAQ and --staged only the README; --word-diff shows [-old-]{+new+}.
Show solution
The README moved from the unstaged diff to the staged diff the moment you ran git add. git diff HEAD --stat would still show both, because it ignores the staging boundary and compares with the last commit. Clean up with git restore --staged README.md && git restore README.md docs/faq.md, or commit.