Git Foundations / Record Your Work

Compare changes with diff

Lesson 8 of 33. Open to read. No account needed.

Why Compare Changes

Before you stage or commit, you want to know exactly what changed: which lines you added, which you removed, and whether anything unintended slipped in. git diff answers that line by line. You used it briefly when splitting a commit; this lesson explains each form of the command and how to read its output.

Four Comparisons

A diff always compares two versions of your files: a before version and an after version. What changes from one command to the next is which two versions are compared.

Four comparisons, each with the before version on the left and the after version on the right. git diff compares the staging area with the working tree: edits not yet staged. git diff --staged compares the last commit with the staging area: what the next commit will record. git diff HEAD compares the last commit with the working tree: everything since that commit. git diff HEAD~1 HEAD compares an older commit with a newer one. New untracked files appear in none of these.

Read each row of the diagram from left to right:

  • git diff compares the staging area with the working tree. It shows edits you have made but not yet staged: what you could still add.
  • git diff --staged compares the last commit with the staging area. It shows what the next commit will record. --cached is another name for the same option, and you will often see it in documentation and online answers.
  • git diff HEAD compares the last commit with the working tree. It shows everything that changed since the last commit, staged or not.
  • git diff HEAD~1 HEAD compares two commits, here the newest commit and its parent. Any names for commits work: IDs, HEAD~n and, once you meet them in the branching module, branch names.
git diff                   # staging area -> working tree
git diff --staged          # last commit -> staging area
git diff HEAD              # last commit -> working tree
git diff HEAD~1 HEAD       # older commit -> newer commit

Two additions work with every form. Put -- <file> at the end to compare only that file, as with git log. Add --stat to see a summary of files and line counts instead of the lines themselves.

Reading a Diff

Here is the diff for one edit to a recipe file, changing the amount of sugar:

diff --git a/recipe.md b/recipe.md
index 079c31f..40bf57a 100644
--- a/recipe.md
+++ b/recipe.md
@@ -4,5 +4,5 @@ Boil 2 cups of water.
 Add 4 crushed cardamom pods.
 Add 2 teaspoons of tea leaves.
 Add 1 cup of milk.
-Add 1 teaspoon of sugar.
+Add 2 teaspoons of sugar.
 Strain into cups.

From the top:

  • diff --git a/recipe.md b/recipe.md starts the section for one file. a/ labels the before version and b/ the after version.
  • index 079c31f..40bf57a 100644 gives short IDs for the stored before and after versions of the file, and its mode: 100644 is an ordinary file. You can usually skip this line.
  • --- a/recipe.md and +++ b/recipe.md say that lines starting with - come from the before version and lines starting with + from the after version.
  • @@ -4,5 +4,5 @@ is a hunk header. A hunk is one block of nearby changes, and a file with changes far apart has several. -4,5 means the hunk shows 5 lines of the before version, starting at line 4. +4,5 means it shows 5 lines of the after version, also starting at line 4.
  • Anything after the closing @@, here Boil 2 cups of water., is not part of the change. Git repeats an earlier line of the file there to help you find your place. In program code it is usually the name of the function the change is in.

Then come the lines themselves. Each starts with one character:

First characterMeaning
A spaceContext: unchanged, shown to help you see where the change is
-In the before version only: removed
+In the after version only: added

A changed line appears as a removal followed by an addition, as the sugar line does here. By default Git shows up to three context lines on each side of a change. In most terminals, removed lines are red and added lines green.

Why a Diff Can Be Empty

git diff printing nothing does not always mean nothing changed. Two common reasons:

  1. The changes are staged. Plain git diff compares the staging area with the working tree. Once you stage an edit, the two match, so it shows nothing. git diff --staged shows the change.
  2. The file is new. A diff compares versions Git already has. A new, untracked file has no version in the staging area or the last commit, so none of the four forms shows it. git status lists it under "Untracked files". Once you add it, git diff --staged shows it as a new file: the header includes new file mode, the before side is --- /dev/null, meaning there was no earlier version, and every line starts with +.

Exercise: Predict and Read Diffs

You will put a repository into several states. Before running each command, predict what it will show, then run it and compare.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup. It makes two commits: a chai recipe, then a change adding cardamom.

    mkdir -p ~/git-practice/read-diffs
    cd ~/git-practice/read-diffs
    git init -b main
    cat > recipe.md <<'EOF'
    # Masala chai
    
    Boil 2 cups of water.
    Add 2 teaspoons of tea leaves.
    Add 1 cup of milk.
    Add 1 teaspoon of sugar.
    Strain into cups.
    EOF
    git add recipe.md
    git commit -m "Add chai recipe"
    cat > recipe.md <<'EOF'
    # Masala chai
    
    Boil 2 cups of water.
    Add 4 crushed cardamom pods.
    Add 2 teaspoons of tea leaves.
    Add 1 cup of milk.
    Add 1 teaspoon of sugar.
    Strain into cups.
    EOF
    git commit -am "Add cardamom"
    
  2. Compare the two commits. Predict: which line will start with +, and will any line start with -?

    git log --oneline
    git diff HEAD~1 HEAD
    git diff HEAD~1 HEAD --stat
    

    The cardamom line is the only + line, and no line starts with -. The hunk header is @@ -1,6 +1,7 @@: 6 lines of the older commit, 7 of the newer one. --stat reports 1 file changed, 1 insertion(+).

  3. Make an unstaged edit. Change the sugar line:

    cat > recipe.md <<'EOF'
    # Masala chai
    
    Boil 2 cups of water.
    Add 4 crushed cardamom pods.
    Add 2 teaspoons of tea leaves.
    Add 1 cup of milk.
    Add 2 teaspoons of sugar.
    Strain into cups.
    EOF
    

    Predict what each of these shows, then run them:

    git diff
    git diff --staged
    

    git diff shows the hunk from the Reading a Diff section above: the old sugar line as -, the new one as +, with four context lines. git diff --staged prints nothing, because nothing is staged yet.

  4. Stage the edit, then predict both commands again:

    git add recipe.md
    git diff
    git diff --staged
    

    Now the results swap. Plain git diff goes quiet because the staging area and the working tree match. git diff --staged shows the sugar change, because that is what the next commit would record.

  5. Edit again after staging. Add a final line, then predict three commands:

    cat >> recipe.md <<'EOF'
    Serve hot.
    EOF
    git diff
    git diff --staged
    git diff HEAD
    

    Each shows something different:

    • git diff shows only +Serve hot., the edit made after staging.
    • git diff --staged still shows only the sugar change.
    • git diff HEAD shows both, because it compares the last commit with the working tree:
    @@ -4,5 +4,6 @@ Boil 2 cups of water.
     Add 4 crushed cardamom pods.
     Add 2 teaspoons of tea leaves.
     Add 1 cup of milk.
    -Add 1 teaspoon of sugar.
    +Add 2 teaspoons of sugar.
     Strain into cups.
    +Serve hot.
    
  6. Create a new file. Predict whether git diff will mention it:

    cat > shopping.md <<'EOF'
    # Shopping
    
    Cardamom pods
    EOF
    git diff
    git status
    

    git diff shows exactly what it showed in step 5: shopping.md is untracked, so no diff includes it. git status lists it under "Untracked files". It also lists recipe.md twice, under "Changes to be committed" and "Changes not staged for commit", because it has both kinds of change.

Success: your predictions matched in each step, or you can say why they did not. In particular: plain git diff was empty in step 4, the three commands in step 5 showed three different results, and shopping.md appeared only in git status.

If something differs:

What you seeLikely causeNext step
git diff prints nothing, but git status lists a modified fileThe change is stagedRun git diff --staged
A new file is missing from every diffThe file is untrackedCheck git status; after git add, git diff --staged shows it
A : or (END) at the bottom of the screenGit is showing output one page at a timePress Space to scroll or q to quit
unknown revision or path not in the working tree for HEAD~1A setup commit failed, so only one commit existsRun git log --oneline; if one commit is missing, remove the folder and run the setup again
\ No newline at end of file under a lineThat file's last line has no line ending, which some editors allowHarmless; the exercise commands always end the last line
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf read-diffs
    ls
    

Independent task: before cleaning up, continue from step 6:

  1. Run git add shopping.md. Before running git diff --staged, predict which files it will show and whether Serve hot. will be in it. Then check.
  2. In step 5, git diff printed the hunk header @@ -6,3 +6,4 @@. What do its four numbers tell you?
  3. Which single command now shows every change since the last commit, including shopping.md?

Check Your Understanding

  1. You edit a tracked file a.txt, run git add a.txt, then create a new file b.txt. What do git diff, git diff --staged and git diff HEAD each show?
  2. A hunk header reads @@ -10,4 +10,6 @@. What does it tell you about the before and after versions?

Answer and Reasoning

Independent task

  1. git diff --staged shows two files. recipe.md has only the sugar change, and shopping.md appears as a new file: new file mode 100644, a before side of --- /dev/null and three + lines. Serve hot. is not there: you added that line after staging recipe.md, so it is still an unstaged edit that only git diff shows.
  2. The hunk shows 3 lines of the before version starting at line 6, and 4 lines of the after version starting at line 6. The after side has one more line: the added Serve hot.. The text Git printed after the header is an earlier line of the file, not part of the change.
  3. git diff HEAD, which compares the last commit with the working tree. It shows the sugar change, Serve hot. and shopping.md. The new file appears now because it has been added; before git add, no form of git diff showed it.

Understanding check

  1. git diff shows nothing: the edit to a.txt is staged, and b.txt is untracked. git diff --staged shows the a.txt edit. git diff HEAD also shows the a.txt edit. b.txt appears in none of them until it is added; git status is where you see it.
  2. The hunk covers 4 lines of the before version and 6 lines of the after version, both starting at line 10. The after version has two more lines in this hunk: for example, two added lines, or three added and one removed.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy