git diff

See exactly what changed

Lesson 5 of 20 · 8 min · Module 2: Everyday changes

Before you save a snapshot, check what is in it. A diff shows every line you added or removed.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Read a diff
  • Compare unstaged and staged changes
  • Summarize changes with --stat

The idea

git diff shows the changes you have not staged yet. git diff --staged shows what is staged for the next commit. In a diff, lines starting with + were added, lines starting with - were removed, and the lines around them are unchanged context. git diff --stat gives a short summary per file.

echo "Thanks for visiting." >> about.md
git diff
git add about.md
git diff --staged

01Read a diff

A diff shows what changed, line by line. Lines starting with + were added and lines starting with - were removed; the others are unchanged context. A line such as @@ -2,3 +2,4 @@ tells you where in the file the change is.

echo "Thanks for visiting." >> about.md
git diff
Output
diff --git a/about.md b/about.md
index 3b526a7..804b040 100644
--- a/about.md
+++ b/about.md
@@ -2,3 +2,4 @@
 
 I am learning to code, one small step at a time.
 I write about what I learn.
+Thanks for visiting.

02Before and after git add

git diff compares your files with the staging area, so it shows only what you have not staged. After git add, the change has moved: git diff --staged shows what the next commit will contain.

git add about.md
git diff --staged
Output
diff --git a/about.md b/about.md
index 3b526a7..804b040 100644
--- a/about.md
+++ b/about.md
@@ -2,3 +2,4 @@
 
 I am learning to code, one small step at a time.
 I write about what I learn.
+Thanks for visiting.

03A quick summary

git diff --stat lists the changed files with a count of added and removed lines. It is a fast way to check that a commit contains only what you meant.

git diff --staged --stat
Output
 about.md | 1 +
 1 file changed, 1 insertion(+)

Your turn

Add the line Thanks for visiting. to the end of about.md with echo and >>. Look at the change with git diff, stage it, look again with git diff --staged, and commit it as Thank visitors on the about page.

What the checks look for

  • about.md ends with the new line
  • Shows the unstaged change with git diff
  • Shows the staged change with git diff --staged
  • Commits the change
Try it in the editor →

Take this with you

git diff shows unstaged changes; git diff --staged shows what you are about to commit.

Go a little further

Change two files, stage only one, and compare git diff, git diff --staged and git diff HEAD, which shows both.

Learn Git & GitHub for free

20 short lessons with real code and instant checks.

See the whole course →